Last active
July 7, 2016 07:15
-
-
Save rybakit/ec508858e78f93b86b11959e07998a14 to your computer and use it in GitHub Desktop.
chr vs pack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function pack_cc($n) { | |
for ($i = 0; $i < $n; ++$i) { | |
$num = \pack('CC', 0xcc, 200); | |
} | |
} | |
function chr_cc($n) { | |
for ($i = 0; $i < $n; ++$i) { | |
$num = "\xcc".\chr(200); | |
} | |
} | |
function pack_ccc($n) { | |
for ($i = 0; $i < $n; ++$i) { | |
$num = \pack('CCC', 0xc7, 100, 200); | |
} | |
} | |
function chr_ccc($n) { | |
for ($i = 0; $i < $n; ++$i) { | |
$num = "\xcc".\chr(100).\chr(200); | |
} | |
} | |
function pack_cn($n) { | |
for ($i = 0; $i < $n; ++$i) { | |
$num = \pack('Cn', 0xdc, 1234); | |
} | |
} | |
function chr_cn($n) { | |
for ($i = 0; $i < $n; ++$i) { | |
$num = "\xdc".\chr(1234 >> 8).\chr(1234); | |
} | |
} | |
function pack_cnc($n) { | |
for ($i = 0; $i < $n; ++$i) { | |
$num = \pack('CnC', 0xc8, 1234, 42); | |
} | |
} | |
function chr_cnc($n) { | |
for ($i = 0; $i < $n; ++$i) { | |
$num = "\xc8".\chr(1234 >> 8).\chr(1234).\chr(42); | |
} | |
} | |
function pack_w_chars($n) { | |
for ($i = 0; $i < $n; ++$i) { | |
$num = \pack('CnC', 0xc9, 1234, 42); | |
} | |
} | |
function pack_wo_chars($n) { | |
for ($i = 0; $i < $n; ++$i) { | |
$num = "\xc9".\pack('n', 1234).\chr(42); | |
} | |
} | |
function empty_loop($n) { | |
for ($i = 0; $i < $n; ++$i) { | |
} | |
} | |
function getmicrotime() | |
{ | |
$t = gettimeofday(); | |
return ($t['sec'] + $t['usec'] / 1000000); | |
} | |
function start_test() | |
{ | |
ob_start(); | |
return getmicrotime(); | |
} | |
function end_test($start, $name, $overhead = null) | |
{ | |
global $total; | |
global $last_time; | |
$end = getmicrotime(); | |
ob_end_clean(); | |
$last_time = $end-$start; | |
$total += $last_time; | |
$num = number_format($last_time,3); | |
$pad = str_repeat(" ", 24-strlen($name)-strlen($num)); | |
if (is_null($overhead)) { | |
echo $name.$pad.$num."\n"; | |
} else { | |
$num2 = number_format($last_time - $overhead,3); | |
echo $name.$pad.$num." ".$num2."\n"; | |
} | |
ob_start(); | |
return getmicrotime(); | |
} | |
function total() | |
{ | |
global $total; | |
$pad = str_repeat("-", 24); | |
echo $pad."\n"; | |
$num = number_format($total,3); | |
$pad = str_repeat(" ", 24-strlen("Total")-strlen($num)); | |
echo "Total".$pad.$num."\n"; | |
} | |
const N = 10000000; | |
$t0 = $t = start_test(); | |
empty_loop(N); | |
$t = end_test($t, 'empty_loop'); | |
$overhead = $last_time; | |
pack_cc(N); | |
$t = end_test($t, 'pack_cc', $overhead); | |
chr_cc(N); | |
$t = end_test($t, 'chr_cc', $overhead); | |
pack_ccc(N); | |
$t = end_test($t, 'pack_ccc', $overhead); | |
chr_ccc(N); | |
$t = end_test($t, 'chr_ccc', $overhead); | |
pack_cn(N); | |
$t = end_test($t, 'pack_cn', $overhead); | |
chr_cn(N); | |
$t = end_test($t, 'chr_cn', $overhead); | |
pack_cnc(N); | |
$t = end_test($t, 'pack_cnc', $overhead); | |
chr_cnc(N); | |
$t = end_test($t, 'chr_cnc', $overhead); | |
pack_w_chars(N); | |
$t = end_test($t, 'pack_w_chars', $overhead); | |
pack_wo_chars(N); | |
$t = end_test($t, 'pack_wo_chars', $overhead); | |
total(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ php tests/bench_chr_vs_pack.php | |
empty_loop 0.085 | |
pack_cc 1.310 1.225 | |
chr_cc 0.600 0.514 | |
pack_ccc 1.452 1.366 | |
chr_ccc 0.924 0.839 | |
pack_cn 1.297 1.211 | |
chr_cn 0.847 0.761 | |
pack_cnc 1.494 1.409 | |
chr_cnc 1.497 1.412 | |
pack_w_chars 1.481 1.396 | |
pack_wo_chars 1.813 1.728 | |
------------------------ | |
Total 12.801 | |
$ php -v | |
PHP 7.0.5 (cli) (built: Apr 23 2016 10:48:01) ( NTS ) | |
Copyright (c) 1997-2016 The PHP Group | |
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies | |
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment