-
Notifications
You must be signed in to change notification settings - Fork 13
/
benchmark.php
45 lines (36 loc) · 1.49 KB
/
benchmark.php
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
<?php
/**
* Created by PhpStorm.
* User: wangjinxi
* Date: 2019/4/16
* Time: 3:32 AM
*/
if (!function_exists('hrtime')) {
function hrtime(bool $as_number = false)
{
return microtime($as_number);
}
}
// Repeat the operation to make benchmark results less random.
const ITERATIONS = 4;
// Print the amount of nanoseconds taken on average for the functions, as well as the relative amount of time taken compared to json_decode
$result = "filename|json_decode|simdjson_decode|simdjson_is_valid|relative_decode|relative_is_valid\n---|:--:|---:|---:|---:|--:\n";
foreach (glob(__DIR__.'/../jsonexamples/*.json') as $item) {
$jsonString = file_get_contents($item);
$stime = hrtime(true);
for ($i = 0; $i < ITERATIONS; $i++) { simdjson_decode($jsonString, true); }
$etime = hrtime(true);
$simdd_time = (int)(($etime - $stime) / ITERATIONS);
$stime = hrtime(true);
for ($i = 0; $i < ITERATIONS; $i++) { simdjson_is_valid($jsonString); }
$etime = hrtime(true);
$simdi_time = (int)(($etime - $stime) / ITERATIONS);
$stime = hrtime(true);
for ($i = 0; $i < ITERATIONS; $i++) { json_decode($jsonString, true); }
$etime = hrtime(true);
$jsond_time = (int)(($etime - $stime) / ITERATIONS);
$relative_decode = sprintf('%.2fx', $simdd_time / $jsond_time);
$relative_is_valid = sprintf('%.2fx', $simdi_time / $jsond_time);
$result.= basename($item)."|{$jsond_time}|{$simdd_time}|$simdi_time|$relative_decode|$relative_is_valid\n";
}
echo $result;