Niels Niels - 3 years ago 106
PHP Question

Why is PHP7 faster than C++ with Boost

I'm having the following implementation both in PHP and in C++ with Boost. It simply reads a file into a string, separates it by spaces (I want to be able to choose this character) and it runs on a file with 20 mln space-separated random numbers (called "spaces"):

In PHP:

<?php

$a = explode(" ", file_get_contents("spaces"));
echo "Count: ".count($a)."\n";
foreach ($a as $b) {
echo $b."\n";
}


and in C++:

#include <boost/algorithm/string.hpp>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdio.h>

using namespace boost;
using namespace std;

int main(int argc, char* argv[])
{
// ifstream ifs("spaces");

// string s ((istreambuf_iterator<char>(ifs)), (istreambuf_iterator<char>()));
char * buffer = 0;
long length;
string filename = "spaces";
FILE * f = fopen (filename.c_str(), "rb");

if (f)
{
fseek (f, 0, SEEK_END);
length = ftell (f);
fseek (f, 0, SEEK_SET);
buffer = (char*) malloc (length);
if (buffer)
{
size_t t = fread (buffer, 1, length, f);
}
fclose (f);
}
string s(buffer, 0, length);
vector <string> v;

split(v, s, is_any_of(" "));

cout << "Count: " << v.size() << endl;

for (int i = 0; i < v.size(); i++) {
cout << v[i] << endl;
}

}


I compiled it with g++ split.cpp -O2 -o split and running it consistently takes 4.5 seconds on my system and PHP7 4.2 seconds. How can PHP be 8% faster than C++?

Answer Source

Although your question is very specific, I will give an answer that is a touch more generic, and possibly suitable here.

High-level languages tend to be written by very smart and capable programmers. They are highly familiar with the tools they use and are capable of finding great solutions for common case scenarios, solutions that is most cases outperform some code written by an average programmer. Thus, code in PHP that matches a common scenario can outperform a bad implementation even in assembly.

Possible effect of JIT (Just-in-time compilation)

I will explicitly state that this is NOT the case here, since PHP7 doesn't support JIT compilation, but this is a very interesting case that might be relevant to any programmer that thinks interpreted languages are always slower.

Some interpreted languages use a feature called JIT (Just-In-Time compilation). This allows a dynamic translation of the higher level code into machine code during runtime.

Since this process is done during runtime, the compiler is aware of the exact CPU, thus enabling it to choose the most suitable machine code for the mission.

Since languages such as C and C++ are often compiled for a broader selection of CPUs, their code might not be as optimized as the one created by the JIT compiler.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download