PHP — Benchmark times!

Jorge Castro
2 min readMay 30, 2020
Sometimes, speed is everything.

PHP benchmarks

It is a collection of 3 PHP benchmarks

It is tested under PHP 7.4 + 64bits + Windows 64 bits but you could download it and test it by yourself (it is the idea)

Benchmark 1, Reference vs No Reference

Is it fast to use a reference argument or return a value?

function reference(&$array) {
$array['a1']=2;
$array['a2']='bbbb';
$array['a4']=55555;
}
function noReference($array) {
$array['a1']=2;
$array['a2']='bbbb';
$array['a4']=55555;
return $array;
}

Result (smaller is better)

Conclusion

For this case, it’s almost the double of fast to use a reference rather than to return the value.

Bechmark 2 Hash speed

We test the benchmark of the generation of hash.

HEX means that the result is resulted in HEXADECIMAL.

RAW means the result is binary. Sometimes HEX=RAW.

Result (short time is better)

JSON vs Serialize

It benchmark to serialize and de-serialize variables

array

$data=['field1'=>"hello",'field2'=>450,'field3'=>['field4'=>'hello','field5'=>450]];

object StdClass

$data=new stdClass();
$data->field1="hello";
$data->field2=450;
$data->field3=new stdClass();
$data->field3->field4="hello";
$data->field3->field5=450;

object (defined by a class)

$data=new MyClass();
$data->field1="hello";
$data->field2=450;
$data->field3=new MyClass2();
$data->field3->field4="hello";
$data->field3->field5=450;

Result (less is better)

Conclusion

While json_encode and serialize have a similar performance but there is a big difference between json_decode and unserialize. Also, array is still fast

Final Note:

Mileage can vary. Doubts and questions?, don’t be shy and comment.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response