, 3 min read

Performance Comparison C vs. Java vs. Javascript vs. PHP vs. Python vs. Cobol vs. Dart

Original post is here eklausmeier.goip.de/blog/2023/03-25-performance-comparison-c-vs-java-vs-javascript-vs-php-vs-python-vs-cobol-dart.


1. Introduction. This post is a continuation of a previous benchmark in Performance Comparison C vs. Java vs. Javascript vs. LuaJIT vs. PyPy vs. PHP vs. Python vs. Perl. Here we compare

  1. C
  2. Java
  3. JavaScript
  4. Python
  5. Cobol
    • GnuCOBOL
    • gcobol (gcc based COBOL)
  6. Dart

I tried to run IBM COBOL as well, but installation as described in Installing IBM COBOL for Linux on Arch Linux failed.

Testing machine is a Ryzen 7 5700G (Cezanne) with 16 CPUs and 4672.0698 MHz max clock. Operating system is Arch Linux 6.2.6-arch1-1. Source code for the programs is in Performance Comparison C vs. Java vs. Javascript vs. LuaJIT vs. PyPy vs. PHP vs. Python vs. Perl.

2. Results. I again use the n-queens problem, i.e., how many times can n queens be put on an n x n chess board without attacking any other queen. Task at hand is to compute from n=1 to n=13 all possible combinations. For example, for C I call time xdamcnt2 13. I ran the programs multiple times and took the lowest result. Of course, all programs produced exactly the same results -- all computations are integer computations. Expected results are:

n 2 4 6 8 10 12 14
combinations 1 0 0 2 10 4 40 92 352 724 2,680 14,200 73,712 365,596

Runtimes in real and user seconds are given in below table. Times taken with time command.

Language real user
C 0.42 0.42
Java 19.0.2+7 0.59 0.62
GnuCOBOL Sobisch 0.68 0.68
Dart 2.19.2 0.72 0.75
node.js 19.8.0 0.74 0.74
PHP 8.3.3 JIT 6.58 6.54
GnuCOBOL 12.2.1 17.45 17.42
PHP 8.2.4 18.16 18.13
Python 3.9.6 32.53 32.35
gcobol Sobisch 191.13 190.39
gcobol 13.0.0 309.79 305.29

3. Special modifications in COBOL program. Simon Sobisch, in private e-mail from 13-Mar-2023 to me, made below changes to the COBOL program:

  1. No variable with sign any more, i.e., variable is always greater or equal zero
  2. Compiler optimization flag -fno-trunc

Modifications by Simon Sobisch:

21c21
<        77 l             pic s9(8) comp-5.
---
>        77 l             pic 9(8) comp-5.
139,149c134,146
<                compute l = z - A(j)
<                if l = 0 then
<       *            exit section
<                    go to configOK-exit
<                end-if
<                if l < 0 then
<                    compute l = 0 - l
<                end-if
<                if l = k - j then
<       *            exit section
<                    go to configOK-exit
---
>                evaluate true
>                when z < A(j)
>                    move A(j) to l
>                    subtract z from l
>                when z = A(j)
>                    exit section
>                when other
>                    move z to l
>                    subtract A(j) from l
>                end-evaluate
>                add j to l
>                if l = k then
>                    exit section

4. Conclusions.

  1. C is by far the fastest. Faster than Java by a factor of 1.5. Almost two times faster than Dart as its other closest competitor.
  2. Java is roughly 20% faster than Javascript.
  3. JavaScript and Dart have almost the same performance.
  4. Cobol can be made faster than JavaScript and Dart but slightly slower than Java.
  5. PHP 8 is two times faster than Python, but almost 25-times slower than Javascript.
  6. PHP with JIT is almost three times faster than normal PHP.
  7. PHP with JIT enabled is five times faster than Python, but nine times slower than Javascript.

5. Dart program. Dart is a programming language by Google and used for Flutter.

int abs(int x) { return  ((x >= 0) ? x : -x); }

/* Check if k-th queen is attacked by any other prior queen.
   Return nonzero if configuration is OK, zero otherwise.
*/
bool configOkay (int k, List<int> a) {
    int z = a[k];

    for (int j=1; j<k; ++j) {
        int l = z - a[j];
        if (l == 0  ||  abs(l) == k - j) return false;
    }
    return true;
}


int solve (int N, List<int> a) {  // return number of positions
    int cnt = 0;
    int k = a[1] = 1;
    int N2 = N;  //(N + 1) / 2;
    bool flag = false;

    for (;;) {
        if (configOkay(k,a)) {
            if (k < N)  { a[++k] = 1;  continue; }
            else ++cnt;
        }
        flag = false;
        do
            if (a[k] < N)  { a[k] += 1;  flag = true; break; }
        while (--k > 1);
        if (flag) continue;
        a[1] += 1;
        if (a[1] > N2) return cnt;
        a[k=2] = 1;
    }
}


void main (List<String> argv) {
    int NMAX = 100;
    List<int> a = [ for(var i=0;i<100;++i) 0 ];
    int argc = argv.length;

    print(" n-queens problem.\n"
    "   2   4    6     8      10         12           14\n"
    " 1 0 0 2 10 4 40 92 352 724 2680 14200 73712 365596\n"
    );

    int start = 1;
    int end = 0;
    if (argc >= 1)  end = int.parse(argv[0]);
    if (end <= 0  ||  end >= NMAX)  end = 10;
    if (argc >= 2) { start = end;  end = int.parse(argv[1]); }
    if (end <= 0  ||  end >= NMAX)   end = 10;

    for (int n=start; n<=end; ++n)
        print(" D(${n}) = ${solve(n,a)}");
}