janet-lang / janet

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

Update `partition`, `mean` #1283

Closed primo-ppcg closed 11 months ago

primo-ppcg commented 11 months ago

(def a (range 1000)) (timeit-loop [:timeout 1] "partition" (partition 5 a)) (pp (partition 3 (range 11)))

master:
```janet
partition 1.000s, 14.15µs/body
@[(0 1 2) (3 4 5) (6 7 8) (9 10)]

branch:

partition 1.000s, 12.38µs/body
@[(0 1 2) (3 4 5) (6 7 8) (9 10)]

Comments

I spent some time trying to optimize partition specially for dictionary types, but was unable to find anything more efficient than slicing over values.

mean wouldn't necessarily need to be special-cased for lengthable?, as the generic case is only about 10% slower, but it can be done simply.

sogaiu commented 11 months ago

Here are my numbers...

master (a13aeaf9):

$ JANET_PATH=~/src/spork/jpm_tree/lib ~/src/janet/build/janet ~/scratch/mean-partition.janet 
partition 1.000s, 27.67µs/body
@[(0 1 2) (3 4 5) (6 7 8) (9 10)]

mean-partition branch (a7536268):

$ JANET_PATH=~/src/spork/jpm_tree/lib ~/src/janet.primo-ppcg/build/janet ~/scratch/mean-partition.janet 
partition 1.000s, 24.82µs/body
@[(0 1 2) (3 4 5) (6 7 8) (9 10)]

...and some manual invocations:

$ ~/src/janet.primo-ppcg/build/janet
Janet 1.30.0-a7536268 linux/x64/gcc - '(doc)' for help

repl:1:> (partition 2 (fiber/new (fn [] (each i [:a :b :c :d] (yield i)))))
@[(:a :b) (:c :d)]

repl:2:> (mean (fiber/new (fn [] (each i [2 3 5 7 11] (yield i)))))
5.6

repl:3:> (/ 28 5)
5.6

Your changes escaped the test monster again!

primo-ppcg commented 11 months ago

Functions still in my sights:

reduce2
accumulate
accumulate2
kvs
sogaiu commented 11 months ago

May be there could be some tests for the new ability of mean and partition to handle non-lengthable things.

Perhaps something like the following in test/suite-boot.janet?

# #1283  
(assert (deep=
          (partition 2 (fiber/new (fn []
                                      (each i [:a :b :c :d] (yield i)))))
          '@[(:a :b) (:c :d)]))
(assert (= (mean (fiber/new (fn []
                                (each i [2 3 5 7 11] (yield i)))))
           5.6))   
sogaiu commented 11 months ago

Thanks!