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++?