In the last AoC challenge you have to put marbles ...
# announcements
k
In the last AoC challenge you have to put marbles in a circle (linked list), sometimes moving back 7 places from the current position. Stripped version of the my code for that is below. I thought that the straight forward
current.prev.prev.prev.prev.prev.prev.prev
should be the fastest way possible, but out of curiosity I also did it as
generateSequence(current) { it.prev }.take(8).last()
and to my surprise, when I measured times of execution I got this : -
.prev
chain -> 2389ms -
generateSequence
-> 460ms So why is
generateSequence
5 times faster? On my machine I get results like that when looping up to 10mln, after that
generateSequence
suddenly takes 3s to finish.
t
If you are doing AoC, there are quite a few of us in #C87V9MQFK!
k
Thanks! Will link my question there 🙂
k
It appears to hit some worst-case scenario in the interpreter, but after a couple of iterations I get
60ms
for the sequence and
40ms
for the
.prev.prev
thing as expected.
👍 1
k
I know that after JIT kicks in times get better. But how can I verify it is the case and what exactly makes it hit the worse case scenario? Also, is there any way to improve it for a single run (like running it once as .kts)?
k
Maybe running it with native makes it better?