Last active
August 29, 2015 13:57
-
-
Save bukka/9877180 to your computer and use it in GitHub Desktop.
Test for json
This file contains 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 | |
$json = file_get_contents(__DIR__ . '/sample.json'); | |
$rs = json_decode($json); | |
if (!$rs) { | |
var_dump(json_last_error(), json_last_error_msg()); | |
exit; | |
} else { | |
echo "JSON decode success\n"; | |
} | |
$rs_enc = json_encode($rs); | |
if (!$rs_enc) { | |
var_dump(json_last_error(), json_last_error_msg()); | |
exit; | |
} else { | |
echo "JSON encode success\n"; | |
} | |
$rs = jsond_decode($json); | |
if (!$rs) { | |
var_dump(jsond_last_error(), jsond_last_error_msg()); | |
exit; | |
} else { | |
echo "JSOND encode success\n"; | |
} | |
$rs_enc = json_encode($rs); | |
if (!$rs_enc) { | |
var_dump(json_last_error(), json_last_error_msg()); | |
exit; | |
} else { | |
echo "JSOND encode success\n"; | |
} | |
$iters = 1000; | |
/* JSON */ | |
$start = microtime(true); | |
for ($i = 0; $i < $iters; $i++) { | |
json_decode($json); | |
} | |
$end = microtime(true); | |
$duration = $end - $start; | |
printf("JSON decode: time for %d iterations: %f" . PHP_EOL, $iters, $duration); | |
$array = json_decode($json); | |
$start = microtime(true); | |
for ($i = 0; $i < $iters; $i++) { | |
json_encode($array); | |
} | |
$end = microtime(true); | |
$duration = $end - $start; | |
printf("JSON encode: time for %d iterations: %f" . PHP_EOL, $iters, $duration); | |
/* JSOND */ | |
$start = microtime(true); | |
for ($i = 0; $i < $iters; $i++) { | |
jsond_decode($json); | |
} | |
$end = microtime(true); | |
$duration = $end - $start; | |
printf("JSOND decode: time for %d iterations: %f" . PHP_EOL, $iters, $duration); | |
$array = jsond_decode($json); | |
$start = microtime(true); | |
for ($i = 0; $i < $iters; $i++) { | |
jsond_encode($array); | |
} | |
$end = microtime(true); | |
$duration = $end - $start; | |
printf("JSOND encode: time for %d iterations: %f" . PHP_EOL, $iters, $duration); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment