mirror of
https://github.com/peklaiho/madlisp.git
synced 2024-11-26 07:04:27 +00:00
add function: timer
This commit is contained in:
parent
94a69e7b76
commit
5b2691d475
@ -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
|
||||||
|
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user