janet-lang / janet

A dynamic language and bytecode vm
https://janet-lang.org
MIT License
3.45k stars 223 forks source link

Rework `reverse` #1242

Closed primo-ppcg closed 1 year ago

primo-ppcg commented 1 year ago

Similar to the changes in #1241. The result is approximately twice as fast on any size argument.

(use spork/test)

(def a (range 10))
(def b (range 100))
(def c (range 1000))

(timeit-loop [:timeout 1] :a (reverse a))
(timeit-loop [:timeout 1] :b (reverse b))
(timeit-loop [:timeout 1] :c (reverse c))

master:

a 1.000s, 0.4635µs/body
b 1.000s, 3.296µs/body
c 1.000s, 31.49µs/body

branch:

a 1.000s, 0.2932µs/body
b 1.000s, 1.939µs/body
c 1.000s, 17.52µs/body

I try not to have more than one open PR at a time, but I thought I might slip these in before the release.

sogaiu commented 1 year ago

master (ecc4d80a):

$ janet reverse.janet 
a 1.000s, 0.5987µs/body
b 1.000s, 4.195µs/body
c 1.000s, 37.62µs/body

branch (c8c0e112):

$ JANET_PATH=$HOME/.local/lib/janet ~/src/janet.primo-ppcg/build/janet reverse.janet 
a 1.000s, 0.4237µs/body
b 1.000s, 2.636µs/body
c 1.000s, 24.57µs/body

Also tested against some repositories with no issues :+1: