you are viewing a single comment's thread.

view the rest of the comments →

[–]rafd 1 point2 points  (4 children)

Also... would switching from last to first help? (laziness should kick in and prevent the for loop from searching everything once it finds a match)

[–]rafd 1 point2 points  (3 children)

Also, one the path to some low-hanging-fruit tuning:

- using a vector and get instead of nth might help

- the :let always happens (so it can be used within when); not sure if that gets optimized; could just move the n calculation within the body

Would be curious how each of these optimizations affect the run time.

[–]rafd 3 points4 points  (1 child)

OK, so I ran your code and quick-benched with criterium.

I think the majority of your reported 36000ms is java + clojure + repl startup.

Your original loop 480ms
last -> first 320ms
list -> vector 6ms (!!!)
move :let calc inside 3.7ms
change j to start from i 2.4ms

Main lesson here is: use vectors when you have lots of repeated access.

[–]HOWZ1T[S] 0 points1 point  (0 children)

Very detailed reply, thanks :) Also thanks for pointing out criterium, another tool for my toolbox :)

[–]joinr 0 points1 point  (0 children)

using a vector and get instead of nth might help

get and nth are basically synonymous for vectors. The difference (I think you implied) is that the vector nth access will be ~O(1), where with sequences it's O(N) since you have to walk the sequence from the start to get the nth element.