add function: timer

This commit is contained in:
Pekka Laiho 2020-10-17 14:36:02 +07:00
parent 94a69e7b76
commit 5b2691d475
2 changed files with 11 additions and 0 deletions

View File

@ -285,6 +285,7 @@ time | `(time)` | `1592011969` | Return the current unix timestamp using [time](
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).
sleep | `(sleep 2000)` | `null` | Sleep for the given period given in milliseconds using [usleep](https://www.php.net/manual/en/function.usleep). sleep | `(sleep 2000)` | `null` | Sleep for the given period given in milliseconds using [usleep](https://www.php.net/manual/en/function.usleep).
timer | `(timer (fn (d) (sleep d)) 200)` | `0.20010209` | Measure the execution time of a function and return it in seconds.
### Type functions ### Type functions

View File

@ -3,6 +3,7 @@ namespace MadLisp\Lib;
use MadLisp\CoreFunc; use MadLisp\CoreFunc;
use MadLisp\Env; use MadLisp\Env;
use MadLisp\Func;
class Time implements ILib class Time implements ILib
{ {
@ -26,5 +27,14 @@ class Time implements ILib
return null; return null;
} }
)); ));
$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;
}
));
} }
} }