diff --git a/README.md b/README.md index 1d63ba5..be9794d 100644 --- a/README.md +++ b/README.md @@ -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). 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). +timer | `(timer (fn (d) (sleep d)) 200)` | `0.20010209` | Measure the execution time of a function and return it in seconds. ### Type functions diff --git a/src/Lib/Time.php b/src/Lib/Time.php index 908ee66..6a7c2ca 100644 --- a/src/Lib/Time.php +++ b/src/Lib/Time.php @@ -3,6 +3,7 @@ namespace MadLisp\Lib; use MadLisp\CoreFunc; use MadLisp\Env; +use MadLisp\Func; class Time implements ILib { @@ -26,5 +27,14 @@ class Time implements ILib 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; + } + )); } }