Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update partition, mean #1283

Merged
merged 3 commits into from
Sep 9, 2023
Merged

Conversation

primo-ppcg
Copy link
Contributor

  • Speed up partition
  • Allow partition to work with non-lengthable types
  • Allow mean to work with non-lengthable types
(use spork/test)

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

master:

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
Copy link
Contributor

sogaiu commented Sep 8, 2023

Here are my numbers...

master (a13aeaf):

$ 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 (a753626):

$ 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!

Copy link
Member

@pepe pepe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@primo-ppcg
Copy link
Contributor Author

Functions still in my sights:

reduce2
accumulate
accumulate2
kvs

@sogaiu
Copy link
Contributor

sogaiu commented Sep 8, 2023

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
Copy link
Contributor

sogaiu commented Sep 8, 2023

Thanks!

@bakpakin bakpakin merged commit e69954a into janet-lang:master Sep 9, 2023
8 checks passed
@primo-ppcg primo-ppcg deleted the mean-partition branch September 10, 2023 00:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants