Kotlin is fast!

Please excuse this noobish post, but I’m excited!

Average times for the same algorithm:

Python: 4.33 seconds

Kotlin: 0.04 seconds

Wow! That’s from a noob who so far knows nothing about optimization specific to Kotlin/JVM.

Non-essential detail:

There’s a theorem that for any irrational number there exists an infinite series of accurate rational approximations a/q, where accurate means that the difference between the irrational number and the rational approximation is less than 1/q2.

A while back I wrote a Python script to find some of those for any given irrational, skipping multiples of previous finds and discarding ones that don’t have better deltas than finds with the same or fewer rational digits. The first version took several minutes to search the space of values for q up to 20,000, but after a series of optimizations I could search up to 1,000,000 in about 8 seconds, then about 6 seconds, and ultimately: 4.33 seconds.

Yesterday I started learning Kotlin, and today I translated that algorithm. The speed improvement floored me. I tested each with pi, tau, e, and √2, and times were essentially unchanged by the targets for the given language.

Anyway, thanks for the encouragement yesterday!

More non-essential detail: I know an interpreted vs compiled comparison isn’t exactly fair, but still. Also, Python does compile to bytecode, and the way I timed it didn’t include that compilation step, only the search algorithm itself.

submitted by /u/5erif
[link] [comments]