Really? Filter beats $read every time for me. I've slightly adjusted your benchmark aliases so that it tells you which method it's benchmarking in the echo.
I've also added a /benchtest command to run each bench 10 times.
alias benchread1 {
var %t $ticks ,%a 1000
while (%a) {
var %l 1,%n 2
while ($read(test.txt, wn, *}}}}}}}}}}*,%l) != $null) && (%n) { noop match the file on line $readn : $v1 | %l = $readn + 1 | dec %n }
dec %a
}
echo -a in $calc($ticks - %t) ms ($read)
}
alias _test if (%n) noop match the file on line $1 : $2- | dec %n
alias benchread2 {
var %t $ticks,%a 1000
while (%a) {
set -u %n 2
filter -fnk test.txt _test *}}}}}}}}}}*
dec %a
}
echo -a in $calc($ticks - %t) ms (/filter)
}
alias benchtest {
var %i = 10
while (%i) {
benchread1
benchread2
if (%i > 1) { linesep -a }
dec %i
}
}
Here's my results for %n = 2:
in 1344 ms ($read)
in 562 ms (/filter)
-
in 1344 ms ($read)
in 578 ms (/filter)
-
in 1359 ms ($read)
in 578 ms (/filter)
-
in 1375 ms ($read)
in 563 ms (/filter)
-
in 1359 ms ($read)
in 578 ms (/filter)
-
in 1344 ms ($read)
in 578 ms (/filter)
-
in 1359 ms ($read)
in 579 ms (/filter)
-
in 1343 ms ($read)
in 578 ms (/filter)
-
in 1344 ms ($read)
in 578 ms (/filter)
-
in 1344 ms ($read)
in 578 ms (/filter)
And with %n = 1:
in 844 ms ($read)
in 578 ms (/filter)
-
in 859 ms ($read)
in 563 ms (/filter)
-
in 843 ms ($read)
in 578 ms (/filter)
-
in 844 ms ($read)
in 563 ms (/filter)
-
in 843 ms ($read)
in 579 ms (/filter)
-
in 843 ms ($read)
in 563 ms (/filter)
-
in 844 ms ($read)
in 562 ms (/filter)
-
in 844 ms ($read)
in 562 ms (/filter)
-
in 844 ms ($read)
in 563 ms (/filter)
-
in 843 ms ($read)
in 578 ms (/filter)