Can Kotlin do this algorithm faster?


#1

So it’s 2019 and I’m looking to learn a new language. It will either be Kotlin or Haskell (maybe Julia).

I don’t have a lot of time, so I want to learn something useful I can use to do math and algorithms. I figure this may be the year to jump into Functional Programming.

I created a fast algorithm to generate twin primes. I already did it (the reference implementation) in Nim, which so far is the fastest. A Rust (about as fast) and D version have also been done by others. I’ve looked some at Kotlin to a assess what it’s good for, but the only way to know for sure is to try it.

But before deciding on which language to learn, I thought I’d ask a few questions about Kotlin’s fit for the algorithm, so let me explain what language features the Nim version provides.

  • a compiled language which can create a standalone executable (which isn’t humongous)
  • able to generate at compile time runtime constant parameters
  • able to perform true parallel operations to use a systems available cores
  • an easy to write in and expressive language (without a lot of coding noise (), {}, etc everywhere)
  • fast coding idioms for loops, iterators, enumerators, etc
  • doesn’t put me through static type casting hell in order to do the math

I think this is enough for now, hope you get the picture.

Below is a link to the Nim code for the algorithm:

And a link to my paper that explains how|why the algorithm works, and the math used to created it.

I see more buzz (articles, blog posts) about Kotlin, so maybe this is the year to start checking it out.

I’d appreciate any feedback with regards to this.

Thanks in advance.


#2

Here are my responses, but my experience is limited…

Yup

What do you mean by generate? There are not really macros per se, lots of people have code generation steps that run before build.

I’ll defer to https://github.com/JetBrains/kotlin-native/blob/master/CONCURRENCY.md and others that may know more

Yup, within reason. Most similar to Rust (but obvious differences, e.g. semicolons vs return, Kotlin lambdas always require braces, etc). More opinionated than D and Nim, and parens and braces and the like are rarely optional. But due to receiver designation, extension methods, and not requiring lambda params to be in parens you can still make decent DSLs. Still, you won’t get that Nim/Python/Coffeescript-like look here.

Fast as in performance or dev time? Unsure on the former, but the latter - Yup.

Depends on what you mean. In general, you can do some math on unrelated types and some not, but I’d call it “conversion hell” and Kotlin does suffer from some numeric limitations from being primarily built to interop with JVM primitives.


#3

First you need to understand the difference between the algorithm, its implementation and optimization performed by compiler. Kotlin and JVM back-end in particular has one of the best compilers existing, which means it will make effective optimizations and the code will be on par with native optimized code. It won’t generate native executable out of the box because it runs on VM (the same goes for Julia).

So, without writing a full article, let me summarize perspectives for languages, you mentioned.

  • Haskell has a very good compiler, but is very hard to learn and gives little control about how your code performs. For pure mathematical application it could be good if you can manage to learn it properly.
  • Julia is very good solution for numerical mathematics. It won’t be the fastest and have rather limited support for parallel computation, but it is very easy for “algorithms” and have a lot of mathematical libraries.
  • Rust should be the fastest solution possible if used properly, but you have to know how to use it. It is very good for system programming, but nos so much for anything else.
  • Kotlin provides the best parallel and asynchronous capabilities and in most cases will generate high-performance code (maybe worse, than Rust, but faster than Julia). The only real performance problem I know is boxing, which arises when you try to write generic algorithms for all types of numbers. What is important is that it is universal. Meaning you can use the same language for wide variety of tasks.

So my verdict is following: if you want small algorithms without bothering with build system and rather simple language, go for Julia. If you want something more universal - Kotlin is your choice. Mathematics and scientific ecosystem in kotlin is very young, but it is growing fast.