Compare C++, JS, Python, Python + numba, PHP7, PHP8, and Golang in “Prime Number” calculation.

Roman Romadin
ITNEXT
Published in
2 min readDec 7, 2020

--

All top programming languages have proved their positions and “decided” on the niches of their use.
However, it is important for every programmer to have an understanding of the quantitative characteristics for each of the languages they use.
You can measure quite a lot of parameters for different purposes.
For some tasks, it will be more important to have a quick calculation of mathematical operations. And for others, faster work with the network and files is more useful.

In this article, we will look at program acceleration using JIT compilation for Python and PHP.

As a calculation task, let’s take the function of checking: whether the number is Prime or Not — “is prime”.

There is you can see the series of Prime Numbers:

Let’s take the basic algorithm for checking: the number is not even and is not divisible by a smaller number up to the root of the desired number (that is, in the loop, go from 3 to the root of the number).
We will need to calculate a number of Prime numbers-up to the maximum. The maximum number in this task will be 10,000,000.

In the algorithm and in the code below, you can see that I did not use parallelization, for a more “honest” estimate of execution time.

The machine on which the programs were launched:

Consider the options for the algorithm in different programming languages:

C++

Go (golang)

Node.js

PHP

I try to use both versions of PHP: 7.3 and 8. To compare PHP without JIT and with them.
I have to say that without enabled JIT PHP8 showed almost the same execution time — about 20 seconds.

Python (without “numba”)

Numba is a just-in-time compiler for Python that works best on code that uses NumPy arrays and functions, and loops.

Python with”numba”

Note: less is better.

Here you can see that the JS version is quite “fast”.
And also: python3 + numba showed a huge increase in performance! Ahead of even Go. Cool!

PHP8 with JIT performed well and comparable to Go!

Probably, something went out of my field of vision and in this task, I did not take into account some point and js in the basic version was faster than Go.

What do you think about this? What can I add or change to complete the experiment?

Are you interested in performance indicators for working with text, images, and the web?

--

--