For or Foreach? PHP vs. Javascript, C++, Java, HipHop (update: HHVM v2)
Lessons learned:
Here is a sample script, running on a 1.4 GHz machine with PHP 5.4.0:
Here are some results from the HipHop compiler and HHVM (06/2012, Ubuntu 12.04, 64bit, AMD E-350):
Here are some results from the HHVM v2.0.2 (05/2013, Ubuntu 12.04, 64bit, 3.4 GHz): Using HHVM instead of PHP does not give big improvements using one thread. Using a better CPU speeds up the code by a factor of 4-5.
With Javascript (node.js) you'll get similar values:
example.js (run: node example.js)
With C++ (gcc 4.6 win32) you'll also get similar values:
example.cpp (run: g++ -o example example.cpp && ./example)
And finally Java (Java 1.7 win64):
example.java (run: javac example.java && java example)
- Foreach is 4-5 times faster than For
- Nested Foreach is 4-5 times faster than nested For
- Foreach with key lookup is 2 times slower than Foreach without
- C++ is 60 times faster than PHP running For/Foreach on Arrays
- Javascript is 2-20 times slower than C++/Java running For on Arrays
- HipHop is currently no alternative to C++
- Using a better CPU makes the code 4-5 times faster
Here is a sample script, running on a 1.4 GHz machine with PHP 5.4.0:
// init arrays
$array = array();
for ($i=0; $i<50000; $i++) $array[] = $i*2;
$array2 = array();
for ($i=20000; $i<21000; $i++) $array2[] = $i*2;
// test1: foreach big-array (foreach small-array)
$start = microtime(true);
foreach ($array as $val) {
foreach ($array2 as $val2) if ($val == $val2) {}
}
echo (microtime(true)-$start)."\n";
// test1b: foreach big-array (foreach small-array)
$start = microtime(true);
foreach ($array as $val) {
foreach ($array2 as $val2) if ($val === $val2) {}
}
echo (microtime(true)-$start)."\n";
// test2: foreach small-array (foreach big-array)
$start = microtime(true);
foreach ($array2 as $val2) {
foreach ($array as $val) if ($val == $val2) {}
}
echo (microtime(true)-$start)."\n";
// test3: foreach big-array (foreach small-array) with key lookup
$start = microtime(true);
foreach ($array as $key=>$val) {
foreach ($array2 as $key2=>$val2) if ($array[$key] == $array2[$key2]) {}
}
echo (microtime(true)-$start)."\n";
// test4: foreach small-array (foreach big-array) with key lookup
$start = microtime(true);
foreach ($array2 as $key=>$val2) {
foreach ($array as $val) if ($array[$key] == $array2[$key2]) {}
}
echo (microtime(true)-$start)."\n";
// test5: for big-array (for small-array)
$start = microtime(true);
$count = count($array);
$count2 = count($array2);
for ($key=0; $key<$count; $key++) {
for ($key2=0; $key2<$count2; $key2++) if ($array[$key] == $array2[$key2]) {}
}
echo (microtime(true)-$start)."\n";
// test6: for small-array (for big-array)
$start = microtime(true);
$count = count($array);
$count2 = count($array2);
for ($key2=0; $key2<$count2; $key2++) {
for ($key=0; $key<$count; $key++) if ($array[$key] == $array2[$key2]) {}
}
echo (microtime(true)-$start)."\n";
$array = array();
for ($i=0; $i<1000000; $i++) $array[] = $i*2;
// test7: foreach big-array
$start = microtime(true);
foreach ($array as &$val) $val++;
echo (microtime(true)-$start)."\n";
// test8: for big-array
$start = microtime(true);
for ($key=0; $key<count($array); $key++) $array[$key]++;
echo (microtime(true)-$start)."\n";
// test8b: for big-array, doing count() outside the loop!
$start = microtime(true);
$count = count($array);
for ($key=0; $key<$count; $key++) $array[$key]++;
echo (microtime(true)-$start)."\n";
Here are some results from the HipHop compiler and HHVM (06/2012, Ubuntu 12.04, 64bit, AMD E-350):
Here are some results from the HHVM v2.0.2 (05/2013, Ubuntu 12.04, 64bit, 3.4 GHz): Using HHVM instead of PHP does not give big improvements using one thread. Using a better CPU speeds up the code by a factor of 4-5.
With Javascript (node.js) you'll get similar values:
example.js (run: node example.js)
tested with 1.6 GHz and 3.4 GHz
var array = [];
for (i=0; i<50000; i++) array.push(i*2);
var array2 = [];
for (i=20000; i<21000; i++) array2.push(i*2);
// js-test1: for big-array (for small array)
var start = new Date().getTime();
for (key=0; key<array.length; key++) {
for (key2=0; key2<array2.length; key2++) if (array[key] == array2[key2]) {}
}
console.log((new Date().getTime() - start) / 1000); // 2.6s, 0.8s
// js-test2: foreach big-array (foreach small array)
var start = new Date().getTime();
for (key in array) {
for (key2 in array2) if (array[key] == array2[key2]) {}
}
console.log((new Date().getTime() - start) / 1000); // 14.2s, 3.2s
var array = [];
for (i=0; i<1000000; i++) array.push(i*2);
// js-test3: for big-array
var start = new Date().getTime();
for (key=0; key<array.length; key++) array[key]++;
console.log((new Date().getTime() - start) / 1000); // 0.013s, 0.016s
With C++ (gcc 4.6 win32) you'll also get similar values:
example.cpp (run: g++ -o example example.cpp && ./example)
tested with 1.6 GHz and 3.4 GHz
#include <sys/time.h>
#include <stdio.h>
#include <vector>
using namespace std;
main() {
struct timeval start, end;
vector<int> array;
for(int i=0; i < 50000; i++) array.push_back(i*2);
vector<int> array2;
for(int i=20000; i < 21000; i++) array2.push_back(i*2);
gettimeofday(&start, NULL);
for (int key=0; key<array.size(); key++)
for (int key2=0; key2<array2.size(); key2++)
if (array[key] == array2[key2]) {}
gettimeofday(&end, NULL);
printf("%lf\n", (float)(end.tv_sec - start.tv_sec +
(end.tv_usec - start.tv_usec)/1000000.0)); // 1.04s, 0.30s, 0.0000s (-O3)
vector<int> array3;
for(int i=0; i < 1000000; i++) array3.push_back(i*2);
gettimeofday(&start, NULL);
for(int i=0; i < array3.size(); i++) array3[i]++;
gettimeofday(&end, NULL);
printf("%lf\n", (float)(end.tv_sec - start.tv_sec +
(end.tv_usec - start.tv_usec)/1000000.0)); // 0.013s, 0.005s, 0.0008 (-O3)
}
And finally Java (Java 1.7 win64):
example.java (run: javac example.java && java example)
tested with 1.6 GHz and 3.4 GHz
import java.util.ArrayList;
public class example {
public static void main(String[] args) throws Exception {
ArrayList<Integer> array = new ArrayList<Integer>();
for (int i = 0; i < 50000; i++) array.add(i*2);
ArrayList<Integer> array2 = new ArrayList<Integer>();
for (int i = 20000; i < 21000; i++) array2.add(i*2);
long start = System.currentTimeMillis();
for (int key = 0; key < array.size(); key++)
for (int key2 = 0; key2 < array2.size(); key2++)
if (array.get(key).equals(array2.get(key2))) {}
System.out.println((float) (System.currentTimeMillis() - start) / 1000);
// 0.12s (Vector: 10.6s), 0.04s
ArrayList<Integer> array3 = new ArrayList<Integer>();
for (int i = 0; i < 1000000; i++) array3.add(i*2);
start = System.currentTimeMillis();
for (int i = 0; i < array3.size(); i++) array3.set(i, array3.get(i)+1);
System.out.println((float)(System.currentTimeMillis() - start) / 1000);
// 0.04s (Vector: 0.17s), 0.04s
}
}
Comments
Post a Comment