Kotlin/Native vs. C++ vs. Freepascal vs. Python: A Comparison

Measuring time of build, time of running, size of the compiled executable file across codebases

Alex Maryin
Better Programming

--

Photo by Chris Liverani on Unsplash

Hello, everyone! Since Google announced Kotlin as the main programming language for Android development as well as for Google’s own libraries called Jetpack-family, adoption of this language was growing by leaps and bounds.

But Kotlin is not only for Android. It’s also for back-end, multi-platform desktop apps, data science, even games, front-end, and machine-learning.

You could reach very wide-range goals with Kotlin. There is just one small fly in the ointment: you need to grab the whole Java virtual machine with your code to deploy the release. But what goes with Kotlin/Native? Is it ready for building different applications? Has it comparable performance with other languages like C++?

In this series of blog posts, I will try to find the truth and compare Kotlin/Native with C++, ancient Freepascal, and Python.

But wait..why Python? Python code doesn’t compile to native apps, so why is it here? Hmm, maybe just for fun, or because I am familiar with it).

Contents

  1. Simple math with native apps, or who’s so clever tonight? — you are here —
  2. Graphic test with SDL2, or Across the Universe with Norton Commander.

Kotlin

Wiki says that the sieve of Eratosthenes is a popular way to benchmark computer performance. It’s literally what we need. Let’s start a contest for different programming languages which could produce native code and compare them with born math-cruncher, Mr. Python.

I wrote the following code in Kotlin as a pattern for others:

I used here the require function to define a precondition. We also have contracts in Kotlin to specify some behavior of the function. And of course, we have assertions but I think it’s preferable to use in the test suite.

Look, how helpful are Kotlin extension functions. I used here mapIndexedNotNull with a predicate for takeIf extension. Literally, one line of code in functional style replaces loop with list building and conditionals.

And I used Array instead of mutableList here for the purposes of speed and memory saving: the values of arrays are changing on each iteration of the while-loop, what’s why the array is preferable here, it doesn’t allocate a new segment of memory on each step but changes values in place very fast. This structure doesn’t go out from the function scope and using the array is fully safe.

Oh, don’t forget the cherry on top! The measuredTimeMills function from the box. Thanks, Kotlin/Native.

Python

So far so good. The next one we will thrill is Python. Let’s compare their syntax sugar:

As you can see the logic and syntax of Python code and Kotlin code are almost the same. I used assert for precondition, list comprehensions for building of initial boolean array and returned a list of primes. Python has no function for time measurement from the box, but we got a better way with the handmaiden decorator measured_time.

Which sugar do I prefer? On the one side, Kotlin has powerful extensions for any kind of type, and you can combine them as you like and get beautiful chains of functions in the result.

On the other side, Python has the most concise syntax for list comprehensions, but also has the most cumbersome conditionals: I hate to enter all these lines for if… else… if… else…if… else. By the way the brand-new pattern matching presented in Python 3.10 is not even close to when syntax in Kotlin.

C++

I had to arrive at the bottommost part of my mind palace to remember how to get the same algorithm working in the scariest C++:

The algorithm itself is still the same, of course, and even has a similar syntax as any other C-like programming language.

But you have to pay heed to all details of memory allocation, memory release, and all of these pointers, links, etc.

You still could grow high-order functions yourself with C++ templates and lambdas, but it is very painful and it breaks all performance props of C++.

For example, using a list as the value argument as a result of the function instead of a pointer adds a hundred milliseconds on each million primes.

Once upon ago… in the far-far galaxy… Every student around the world was learning Pascal. I think we should pay tribute to Pascal for having millions of bearded coders at the present who maintains Delphi (what is its name now?) and Lazarus IDEs. Notepad amateurs, as well as vim’ers, should appreciate the Spartan advantages of Lazarus IDE without any modern auto-completion or code suggestions.

Pascal code is the longest one. It has generics but, as you see, they managed to complicate it. (why specialize? What is this word for?)

Pascal still hasn’t any opportunities of high-order functions and lambdas. Yes, it has a functional type for arguments, but… You should pass this way yourself.

How do you think what results we will get in this contest? I measured time of build, time of running, size of the compiled executable file, and got the following results for calculating the first of 100,000,000 primes:

С++ lives up to its title of the fastest run and the smallest application. But Pascal is breathing down its neck!

Kotlin/Native file has a half size of Pascal file but lags behind on the half too. And what happens with Python? Why it's so slow? Because it doesn’t use precompiled libraries, but only interprets each line of the code.

I give 5 points to Kotlin code for joining the functional programming power club and having expressive, concise code.

And second place goes to Python. C++ code is more beautiful than Pascal’s in my opinion, but don’t forget that I used it for many years with great pleasure.

In the second part of our contest, I will compare these languages in the graphic test: we will use SDL2 library and make an old screensaver with starfield from Norton Commander. This test is inspired by this post by Vyacheslav Arkhipov.

Stay tuned and I’ll be back.

--

--

My profession is lawyer, but I like programming for a whole my life. It started since 90's with ZX-spectrum, and now I fan of Kotlin, Python, Pascal & C++.