1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
<?php
echo "\nPHP Version: ".phpversion()."\n";
if (extension_loaded('jsond')) {
$ver = "pecl/jsond ".phpversion('jsond');
$suf = "jsond";
$enc = 'jsond_encode';
$dec = 'jsond_decode';
$opt = JSOND_PRETTY_PRINT;
} else if (version_compare(phpversion('json'), '1.3.0', '<')) {
$ver = "ext/json ".phpversion('json');
$suf = "json";
$enc = 'json_encode';
$dec = 'json_decode';
$opt = JSON_PRETTY_PRINT;
} else {
$ver = "pecl/jsonc ".phpversion('json');
$suf = "jsonc";
$enc = 'json_encode';
$dec = 'json_decode';
$opt = JSON_PRETTY_PRINT;
}
echo "Json Version: ".$ver."\n\n";
$ary = get_loaded_extensions();
for($i=0; $i<pow(2, 8); $i++){
// integers
$ary = array_merge($ary, range(0, 1024));
// strings
$ary[] = md5("$i");
// floats
$ary[] = (float)count($ary)/100;
}
echo "Count: ".count($ary)."\n";
$a = microtime(true);
$jsoned = $enc($ary);
$b = microtime(true);
printf("Encode in %.5f sec, %ld bytes\n", $b-$a, strlen($jsoned));
file_put_contents("bench.$suf", $enc($ary, $opt));
$a = microtime(true);
$json = $dec($jsoned);
$b = microtime(true);
if ($json) {
printf("Decode in %.5f sec\n", $b-$a);
} else {
echo "Decode not implemented\n";
}
/*
if (class_exists("JsonIncrementalParser")) {
$a = microtime(true);
$json = json_decode(file_get_contents("bench.json"));
$b = microtime(true);
if ($json) {
printf("Decode from memory in %.5f sec\n", $b-$a);
}
$a = microtime(true);
$p = new JsonIncrementalParser();
$p->parseFile("bench.json");
$json = $p->get();
$b = microtime(true);
if ($json) {
printf("Decode from file in %.5f sec\n", $b-$a);
}
}
*/
|