Kotlin/Native vs. C++ vs. Freepascal vs. Python: A Comparison | by Alex Maryin | Apr, 2022

Measuring time of construct, time of working, dimension of the compiled executable file throughout codebases

Picture by Chris Liverani on Unsplash

Hiya, everybody! Since Google introduced Kotlin as the primary programming language for Android growth in addition to for Google’s personal libraries referred to as Jetpack-family, adoption of this language was rising by leaps and bounds.

However Kotlin isn’t just for Android. It’s additionally for back-end, multi-platform desktop apps, information science, even video games, front-end, and machine-learning.

You might attain very wide-range objectives with Kotlin. There is only one small fly within the ointment: you want to seize the entire Java digital machine together with your code to deploy the discharge. However what goes with Kotlin/Native? Is it prepared for constructing completely different functions? Has it comparable efficiency with different languages like C++?

On this sequence of weblog posts, I’ll attempt to discover the reality and evaluate Kotlin/Native with C++, historic Freepascal, and Python.

However wait..why Python? Python code doesn’t compile to native apps, so why is it right here? Hmm, perhaps only for enjoyable, or as a result of I’m accustomed to it).

  1. Simple arithmetic with native apps, or who’s so intelligent tonight? — you might be right here —
  2. Graphic check with SDL2, or Throughout the Universe with Norton Commander.

Kotlin

Wiki says that the sieve of Eratosthenes is a well-liked strategy to benchmark pc efficiency. It’s actually what we want. Let’s begin a contest for various programming languages which may produce native code and evaluate them with born math-cruncher, Mr. Python.

I wrote the next code in Kotlin as a sample for others:

I used right here the require perform to outline a precondition. We even have contracts in Kotlin to specify some conduct of the perform. And naturally, we now have assertions however I believe it’s preferable to make use of within the check suite.

Look, how useful are Kotlin extension capabilities. I used right here mapIndexedNotNull with a predicate for takeIf extension. Actually, one line of code in useful model replaces loop with record constructing and conditionals.

And I used Array as an alternative of mutableList right here for the needs of pace and reminiscence saving: the values of arrays are altering on every iteration of the whereas-loop, what’s why the array is preferable right here, it doesn’t allocate a brand new section of reminiscence on every step however modifications values in place very quick. This construction doesn’t exit from the perform scope and utilizing the array is absolutely protected.

Oh, don’t neglect the cherry on prime! The measuredTimeMills perform from the field. Thanks, Kotlin/Native.

Python

Thus far so good. The following one we are going to thrill is Python. Let’s evaluate their syntax sugar:

As you possibly can see the logic and syntax of Python code and Kotlin code are nearly the identical. I used assert for precondition, record comprehensions for constructing of preliminary boolean array and returned a listing of primes. Python has no perform for time measurement from the field, however we obtained a greater means with the handmaiden decorator measured_time.

Which sugar do I desire? On the one aspect, Kotlin has highly effective extensions for any form of kind, and you may mix them as you want and get lovely chains of capabilities within the consequence.

On the opposite aspect, Python has probably the most concise syntax for record comprehensions, but in addition has probably the most cumbersome conditionals: I hate to enter all these traces for if… else… if… else…if… else. By the way in which the brand-new sample matching offered in Python 3.10 isn’t even near when syntax in Kotlin.

C++

I needed to arrive on the bottommost a part of my thoughts palace to recollect get the identical algorithm working within the scariest C++:

The algorithm itself continues to be the identical, after all, and even has an identical syntax as every other C-like programming language.

However it’s a must to pay heed to all particulars of reminiscence allocation, reminiscence launch, and all of those pointers, hyperlinks, and many others.

You continue to may develop high-order capabilities your self with C++ templates and lambdas, however it is extremely painful and it breaks all efficiency props of C++.

For instance, utilizing a listing as the worth argument on account of the perform as an alternative of a pointer provides 100 milliseconds on every million primes.

As soon as upon in the past… within the far-far galaxy… Each pupil around the globe was studying Pascal. I believe we should always pay tribute to Pascal for having hundreds of thousands of bearded coders at present who maintains Delphi (what’s its title now?) and Lazarus IDEs. Notepad amateurs, in addition to vim’ers, ought to respect the Spartan benefits of Lazarus IDE with none trendy auto-completion or code ideas.

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

Pascal nonetheless hasn’t any alternatives of high-order capabilities and lambdas. Sure, it has a useful kind for arguments, however… You must cross this manner your self.

How do you assume what outcomes we are going to get on this contest? I measured time of construct, time of working, dimension of the compiled executable file, and obtained the next outcomes for calculating the primary of 100,000,000 primes:

С++ lives as much as its title of the quickest run and the smallest software. However Pascal is respiratory down its neck!

Kotlin/Native file has a half dimension of Pascal file however lags behind on the half too. And what occurs with Python? Why it is so sluggish? As a result of it doesn’t use precompiled libraries, however solely interprets every line of the code.

I give 5 factors to Kotlin code for becoming a member of the useful programming energy membership and having expressive, concise code.

And second place goes to Python. C++ code is extra lovely than Pascal’s for my part, however don’t neglect that I used it for a few years with nice pleasure.

Within the second a part of our contest, I’ll evaluate these languages within the graphic check: we are going to use SDL2 library and make an outdated screensaver with starfield from Norton Commander. This check is impressed by this post by Vyacheslav Arkhipov.

Keep tuned and I’ll be again.

More Posts