add mtime, move timer from core to madlisp

This commit is contained in:
Pekka Laiho 2020-12-11 21:30:34 +07:00
parent 83c1e54463
commit dc6c206f4e
4 changed files with 7 additions and 12 deletions

View File

@ -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. 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). 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. 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 ### Collection functions
@ -582,6 +581,7 @@ Note that support for multibyte characters in strings is limited because the pro
Name | Example | Example result | Description Name | Example | Example result | Description
------- | ------- | -------------- | ----------- ------- | ------- | -------------- | -----------
time | `(time)` | `1592011969` | Return the current unix timestamp using [time](https://www.php.net/manual/en/function.time). 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). 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). 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).

2
mad/misc.mad Normal file
View File

@ -0,0 +1,2 @@
;; Measure how long it takes to execute f
(defn timer (f) (let (st (mtime)) (f) (- (mtime) st)))

View File

@ -110,16 +110,5 @@ class Core implements ILib
throw new MadLispUserException($error); 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;
}
));
}
} }
} }

View File

@ -13,6 +13,10 @@ class Time implements ILib
fn () => time() 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, $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()) fn (string $format, ?int $time = null) => date($format, $time !== null ? $time : time())
)); ));