PHP7 is 2-3 times faster. Is it finally a breakdown?

Comparison of efficiency ANSI C, Golang and PHP7 based on Monte Carlo simulation.

Posted by Lukasz D. Tulikowski on October 7, 2016


Efficiency of PHP7 alongside others languages.

Beside some other claiming to be fast programming language. C is compilable; there is no wonder here. However, PHP7 alongside Golang script to come out as a surprise, and the high failure on PHP7 efficiency.

I’ve started with PHP - and fist problem appeared. Should I use mt_rand() or rand() function? I wanted my solution to be as fast as, so I compared those two functions. Here are the results:

rand()

# of trials= 100000000 , estimate of pi is 3.14167

real    0m20.411s
user    0m20.090s
sys        0m0.124s

mt_rand()

# of trials= 100000000 , estimate of pi is 3.14151

real    0m20.362s
user    0m20.027s
sys        0m0.131s

To sum up, mt_rand() is as fast as rand(), but better entropy. So I choose mt_rand() and here is few lines of code I wrote:

<?php
$niter = intval($argv[1]);
$count = 0;
for ($i = 0; $i < $niter; $i++){
    $x = (double) mt_rand() / getrandmax();
    $y = (double) mt_rand() / getrandmax();
    $z = $x * $x + $y * $y;
    if ($z <= 1) {
        $count++;
    }
}
$pi = (double) $count / $niter * 4;
printf ("# of trials= %d , estimate of pi is %g \n", $niter, $pi);

Because ANSI C syntax is very similar to PHP my second I wrote Monte Carlo simulation in C.

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>

int main (int argc, char *argv[])
{
  double x, y, z, pi;
  int i, count = 0;
  int niter = strtol (argv[1], NULL, 0);
  srand (time (0));
  count = 0;
  for (i = 0; i < niter; i++){
    x = (double) rand () / RAND_MAX;
    y = (double) rand () / RAND_MAX;
    z = x * x + y * y;
    if (z <= 1)
    count++;
  }
  pi = (double) count / niter * 4;
  printf ("# of trials= %d , estimate of pi is %g \n", niter, pi);
  return 0;
}

The most demanding were the syntax of Golang, which still is a little bit new to me. Also, I was surprised how easy is to write in Golang. Moreover, below is Monte Carlo method in Golang.

package main

import (
    "fmt"
    "math/rand"
    "os"
    "strconv"
    "time"
)

func main() {
    var niter int
    niter, _ = strconv.Atoi(os.Args[1])
    count := 0
    rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
    for i := 0; i < niter; i++ {
        x := rnd.Float64()
        y := rnd.Float64()
        z := x*x + y*y
        if z <= 1 {
            count++
        }
    }
    pi := float64(count) / float64(niter) * float64(4)
    fmt.Printf("# of trials= %d , estimate of pi is %g \n", niter, pi)
}

Finally, I ran each program and measured the time of execution. Results came as a surprise.

PHP Monte Carlo method run and timing

time php pi-monte-carlo.php 10000000

# of trials= 100000000 , estimate of pi is 3.142

real    0m41.841s
user    0m39.164s
sys     0m0.024s

ANSI C Monte Carlo method run and timing

gcc -o pi-monte-carlo pi-monte-carlo.c

time ./pi-monte-carlo 100000000

# of trials= 100000000 , estimate of pi is 3.14165

real    0m3.829s
user    0m3.680s
sys     0m0.008s

Golang Monte Carlo method run and timing

time go run pi-monte-carlo.go 10000000

# of trials= 100000000 , estimate of pi is 3.14184392

real    **0m4.549s**
user    0m4.356s
sys     0m0.036s
PHP7       41.8 sec.

ANSI C     3.8  sec.

Golang     4.6  sec.

What do you think about it? PHP7 still seems to be ten times slower than edge solutions.