mirror of
https://github.com/peklaiho/madlisp.git
synced 2024-11-22 13:24:46 +00:00
add mtime, move timer from core to madlisp
This commit is contained in:
parent
83c1e54463
commit
dc6c206f4e
@ -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).
|
||||
|
||||
|
2
mad/misc.mad
Normal file
2
mad/misc.mad
Normal file
@ -0,0 +1,2 @@
|
||||
;; Measure how long it takes to execute f
|
||||
(defn timer (f) (let (st (mtime)) (f) (- (mtime) st)))
|
@ -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;
|
||||
}
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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())
|
||||
));
|
||||
|
Loading…
Reference in New Issue
Block a user