From dc6c206f4e87b6620f6167f1d12501a01b6389ce Mon Sep 17 00:00:00 2001 From: Pekka Laiho Date: Fri, 11 Dec 2020 21:30:34 +0700 Subject: [PATCH] add mtime, move timer from core to madlisp --- README.md | 2 +- mad/misc.mad | 2 ++ src/Lib/Core.php | 11 ----------- src/Lib/Time.php | 4 ++++ 4 files changed, 7 insertions(+), 12 deletions(-) create mode 100644 mad/misc.mad diff --git a/README.md b/README.md index 5ac3e62..7644451 100644 --- a/README.md +++ b/README.md @@ -415,7 +415,6 @@ pstr | yes | `(pstr {"a":"b"})` | `"{\"a\":\"b\"}"` | Print expression to strin read | yes | `(read "(+ 1 2 3)")` | `(+ 1 2 3)` | Read a string as code and return the expression. sleep | no | `(sleep 2000)` | `null` | Sleep for the given period given in milliseconds using [usleep](https://www.php.net/manual/en/function.usleep). throw | yes | `(throw "invalid value")` | `error: "invalid value"` | Throw an exception. The given value is passed to catch. See the section Exceptions. -timer | no | `(timer (fn (d) (sleep d)) 200)` | `0.20010209` | Measure the execution time of a function and return it in seconds. ### Collection functions @@ -582,6 +581,7 @@ Note that support for multibyte characters in strings is limited because the pro Name | Example | Example result | Description ------- | ------- | -------------- | ----------- time | `(time)` | `1592011969` | Return the current unix timestamp using [time](https://www.php.net/manual/en/function.time). +mtime | `(mtime)` | `1607696761.132` | Return the current unix timestamp as float that includes microseconds. Uses [microtime](https://www.php.net/manual/en/function.microtime). date | `(date "Y-m-d H:i:s")` | `"2020-06-13 08:33:29"` | Format the current time and date using [date](https://www.php.net/manual/en/function.date.php). strtotime | `(strtotime "2020-06-13 08:34:47")` | `1592012087` | Parse datetime string into unix timestamp using [strtotime](https://www.php.net/manual/en/function.strtotime.php). diff --git a/mad/misc.mad b/mad/misc.mad new file mode 100644 index 0000000..a28211c --- /dev/null +++ b/mad/misc.mad @@ -0,0 +1,2 @@ +;; Measure how long it takes to execute f +(defn timer (f) (let (st (mtime)) (f) (- (mtime) st))) diff --git a/src/Lib/Core.php b/src/Lib/Core.php index 88490f1..c574214 100644 --- a/src/Lib/Core.php +++ b/src/Lib/Core.php @@ -110,16 +110,5 @@ class Core implements ILib throw new MadLispUserException($error); } )); - - if (!$this->safemode) { - $env->set('timer', new CoreFunc('timer', 'Measure the execution time of a function and return it in seconds.', 1, -1, - function (Func $f, ...$args) { - $start = microtime(true); - $f->call($args); - $end = microtime(true); - return $end - $start; - } - )); - } } } diff --git a/src/Lib/Time.php b/src/Lib/Time.php index 746c707..ced0eba 100644 --- a/src/Lib/Time.php +++ b/src/Lib/Time.php @@ -13,6 +13,10 @@ class Time implements ILib fn () => time() )); + $env->set('mtime', new CoreFunc('mtime', 'Return the current unix timestamp with microseconds as float.', 0, 0, + fn () => microtime(true) + )); + $env->set('date', new CoreFunc('date', 'Format the time according to first argument.', 1, 2, fn (string $format, ?int $time = null) => date($format, $time !== null ? $time : time()) ));