Let's compare C ++, JS, Python, Python + numba, PHP7, PHP8, and Golang using the example of calculating "Prime Number"

All top programming languages ​​have long proved their positions and "determined" with 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 ​​that he uses.





Quite a few parameters can be measured 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 speeding up a program using JIT compilation for the Python and PHP languages.





As a task for the calculation, let's take the function of checking whether a number is Prime or not - "is prime". Let's take a basic algorithm for checking that the number is Prime:





  • the number is not even





  • and is not divisible by a smaller number up to the root of the desired one (that is, in the loop we go from 3 to the root of the number)





We will need to calculate a series of Prime numbers - up to the maximum. The maximum number in this problem would 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" estimation of the execution time.





The machine on which the launches were made:





 :                     MacBook Pro
 :                MacBookPro14,1
 :                     Dual-Core Intel Core i5
 :                 2,3 GHz
 :              1
  :              2
 2-  (  ):    256 
 3- :                  4 
 Hyper-Threading:       
:                          8 
 Boot ROM:                 428.0.0.0.0
 SMC ():              2.43f10
      
      



:





C++

g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
      
      



#include <iostream>
#include <cmath>
#include <time.h>

using namespace std;

bool isPrime(int num)
{
    if (num == 2) {
        return true;
    }
    if (num <= 1 || num % 2 == 0) {
        return false;
    }

    double sqrt_num = sqrt(double(num));
    for (int div = 3; div <= sqrt_num; div +=2)
    {
        if (num % div == 0) {
            return false;
        }
    }
    return true;
}


int main()
{
    int N = 10000000;
    clock_t start, end;
    start = clock();
    for (int i = 0; i < N; i++) {
        isPrime(i);
    }
    end = clock();
    cout << (end - start) / ((double) CLOCKS_PER_SEC);
    cout << " sec \n";
    return 0;
}
      
      



Go (golang)





go version
go version go1.15.4 darwin/amd64
      
      



package main

import (
   "fmt"
   "math"
   "time"
)

func isPrime(num int) bool {
   if num == 2 {
      return true
   }
   if num == 1 || num%2 == 0 {
      return false
   }
   to := int(math.Sqrt(float64(num)))
   for div := 3; div <= to; div += 2 {
      if num%div == 0 {
         return false
      }
   }
   return true
}

func do(N int) {
   for i := 0; i < N; i++ {
      prime := isPrime(i)
      if prime {
         // fmt.Printf("%+v: %+v\n", i, prime)
      }
   }
}

func main() {
   st := time.Now()
   do(10_000_000)
   fmt.Printf("%+v\n", time.Since(st))
}
      
      



Node.js

node --version  
v15.0.1
      
      



function isPrime(num) {
    if (num === 2) {
        return true;
    }
    if (num <= 1 || num % 2 === 0) {
        return false
    }
    for (let div = 3; div <= Math.sqrt(num); div += 2) {
        if (num % div === 0) {
            return false;
        }
    }
    return true;
}

function main(N) {
    const st = new Date().getTime();
    for (let i = 0; i < N; i++) {
        let prime = isPrime(i);
        if (prime) {
            // console.log(i + ': ' + prime);
        }
    }
    console.log((new Date().getTime() - st) / 1000);
}

(function (){
    const N = 10_000_000;
    main(N)
})()
      
      



PHP

<?php

function isPrime($num)
{
    if ($num == 2) {
        return true;
    }
    if ($num == 1 || $num %2 == 0) {
        return false;
    }
    $to = sqrt($num) + 1;
    for ($i = 3; $i <= $to; $i += 2) {
        if ($num % $i == 0) {
            return false;
        }
    }
    return true;
}

function run($N)
{
    for ($i = 0; $i <= $N; $i++) {
        isPrime($i);
    }
}

function main()
{
    $st = microtime(true);
    run(10000000);

    echo microtime(true) - $st;
}

// ,   -))      .
main();
      
      



Python (without "numba")

python3 --version
Python 3.8.5
      
      



import math
from time import perf_counter


def is_prime(num):
    if num == 2:
        return True
    if num == 1 or not num % 2:
        return False
    for div in range(3, int(math.sqrt(num)) + 1, 2):
        if not num % div:
            return False
    return True


def do(n):
    for i in range(n):
        is_prime(i)


if __name__ == '__main__':
    N = 10_000_000
    st = perf_counter()
    do(N)
    end = perf_counter()
    print(end - st)
      
      



Python with "numba"

import math
from time import perf_counter

from numba import njit


@njit(fastmath=True)
def is_prime(num):
    if num == 2:
        return True
    if num == 1 or not num % 2:
        return False
    for div in range(3, int(math.sqrt(num)) + 1, 2):
        if not num % div:
            return False
    return True


@njit(fastmath=True)
def do(n):
    for i in range(n):
        is_prime(i)


if __name__ == '__main__':
    N = 10_000_000
    st = perf_counter()
    do(N)
    end = perf_counter()
    print(end - st)
      
      



:





Note: Less is better.
: - .

, JS "".





:





  • python3 + numba ! Go. !





  • PHP8 JIT . Go!





, - - js Go.





, ? ?





, ?








All Articles