-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserialize_bench.php
More file actions
106 lines (89 loc) · 2.42 KB
/
serialize_bench.php
File metadata and controls
106 lines (89 loc) · 2.42 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
function generate_random_string($length) {
$result = '';
$chars = '0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_';
$rand_max = strlen($chars) - 1;
while (--$length >= 0) {
$result .= $chars[mt_rand(0, $rand_max)];
}
return $result;
}
function generate_random_integer_array($keys, $max) {
$result = array();
while (--$keys >= 0) {
$result[] = mt_rand(0, $max);
}
return $result;
}
function generate_random_string_array($keys, $length) {
$result = array();
while (--$keys >= 0) {
$key = generate_random_string(10);
$result[$key] = generate_random_string($length);
}
return $result;
}
function test_serialize($a) {
print("test_serialize: ");
for ($i = 0; $i < 1000; ++$i) {
$s = serialize($a);
}
return $s;
}
function test_unserialize($s) {
print("test_unserialize: ");
for ($i = 0; $i < 1000; ++$i) {
$a = unserialize($s);
}
return $a;
}
function test_json_encode($a) {
print("test_json_encode: ");
for ($i = 0; $i < 1000; ++$i) {
$j = json_encode($a);
}
return $j;
}
function test_json_decode($j) {
print("test_json_decode: ");
for ($i = 0; $i < 1000; ++$i) {
$a = json_decode($j, True);
}
return $a;
}
function bench_f($f, $a) {
$__f = create_function('$a', 'return '.$f.'($a);');
$start = gettimeofday(True);
$r = $__f($a);
$past = gettimeofday(True) - $start;
printf("%f ms\n", $past);
return $r;
}
function bench_integer_array($keys, $size) {
print("\ninteger, value size: $size\n");
$a = generate_random_integer_array($keys, $size);
$s = bench_f('test_serialize', $a);
$r = bench_f('test_unserialize', $s);
$j = bench_f('test_json_encode', $a);
$r = bench_f('test_json_decode', $j);
}
function bench_string_array($keys, $length) {
print("\nstring, value length: $length\n");
$a = generate_random_string_array($keys, $length);
$s = bench_f('test_serialize', $a);
$r = bench_f('test_unserialize', $s);
$j = bench_f('test_json_encode', $a);
$r = bench_f('test_json_decode', $j);
}
try {
mt_srand();
bench_integer_array(10, 100);
bench_integer_array(100, 10000);
bench_integer_array(1000, 1000000);
bench_string_array(10, 10);
bench_string_array(100, 100);
bench_string_array(1000, 100);
bench_string_array(100, 1000);
} catch(Exception $e) {
}
?>