; /benchmark <N> [-icm] <text>
;
; Switches: i = identifiers, c = basic commands, m = multiple commands
;
; With no switches it defaults to evaluating <text> as a command unless
; it contains what appears to be identifiers or variables.
;
; You can suppress the first more technical echo with /.benchmark
;
; Identifiers are tested using /!noop <text>
; Commands are tested using /<text>
; Multiple commands are tested using /!scid $cid <text>
;
; If you wish to benchmark structures such as if statements, while
; loops or /var, you need to use the 'm' switch.
;
; The 'speed factor' is an attempt to standardize the results of these
; benchmarks across a variety of machines by calculating (before and
; after the main benchmark) the time it takes for your PC to perform a
; simple while loop which it then uses in the overall calculation.
; If done correctly, a 'speed factor' should be a property of <text>
; and should be independent of the number of iterations and, more
; importantly, the PC the benchmark is performed on.
;
;
; Basic operations such as the evaluation of $null seem to have
; a speed factor of around 1 or less.
;
; Examples:
;
; /benchmark 20000 -i $crc(text, 0)
; /benchmark 20000 -i $hash(text, 32)
;
; /.benchmark 10000 -m if (8 & 1) noop
; /.benchmark 10000 -m if (2 \\ 8) noop
;
; /benchmark 200 -c /myalias
alias benchmark {
var %i = 50000 | ; number of iterations to use to determine the 'speed factor'
if ($1 !isnum) {
echo -egac info * /benchmark <N> [-icm] <text>
return
}
var %before = %i, %after = %i, %main = $1, %cid = $cid, %identifier, %command, %multi, %out
if ($2 == -c) {
%out = / $+ $3-
%command = $3-
}
elseif ($2 == -i) {
%out = /!noop $3-
%identifier = $3-
}
elseif ($2 == -m) {
%out = /!scid % $+ cid $3-
%multi = $3-
}
elseif ($wildtok($2-, $!*, 1, 32)) || ($wildtok($2-, % $+ *, 1, 32)) {
%out = /!noop $2-
%identifier = $2-
}
else {
%out = / $+ $2-
%identifier = $2-
}
!set -l %time $ticks
while (%before) !dec %before
!set -l %before $ticks - %time
if (%command != $null) {
!set -l %time $ticks
while (%main) {
[ [ %command ] ]
!dec %main
}
!set -l %main $ticks - %time
}
elseif (%multi != $null) {
!set -l %time $ticks
while (%main) {
!scid %cid %multi
!dec %main
}
!set -l %main $ticks - %time
}
else {
!set -l %time $ticks
while (%main) {
!noop [ [ %identifier ] ]
!dec %main
}
!set -l %main $ticks - %time
}
!set -l %time $ticks
while (%after) !dec %after
!set -l %after $ticks - %time
var %total = %before + %after
echo -qgace info First loop: %before $+ ms - Second loop: %after $+ ms - A total of $&
%total $+ ms for $calc(2 * %i) trivial cycles ( $+ $calc(%total / 2 / %i) $+ ms per cycle)
linesep -a
echo -gac info2 Results for $1 cycles of: %out
echo -gac info2 %main $+ ms for $1 cycles ( $+ $calc(%main / $1) $+ ms per cycle) - Speed factor: $&
$calc((%main / $1) / (%total / %i))
linesep -a
}