/
unserialize.php
81 lines (67 loc) · 2.11 KB
/
unserialize.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
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
<?php
/**
* Created by PhpStorm.
* User: ovr
* Date: 06.12.15
* Time: 22:26
*/
use Lavoiesl\PhpBenchmark\Benchmark;
use Ovr\Bench\AssignOrNot\ReturnAfterAssignProperty;
use Ovr\Bench\AssignOrNot\ReturnWithoutAfterAssignProperty;
include_once __DIR__ . '/vendor/autoload.php';
function bench($value, $n = 1000000) {
$benchmark = new Benchmark;
$serialized = serialize($value);
$benchmark->add('unserialize', function() use (&$serialized) {
unserialize($serialized);
});
$jsonEncoded = json_encode($value);
$benchmark->add('json_decode', function() use (&$jsonEncoded) {
json_decode($jsonEncoded);
});
if (function_exists('bin_decode')) {
$binEncoded = bin_encode($value);
$benchmark->add('bin_decode', function() use (&$binEncoded) {
bin_decode($binEncoded);
});
}
if (function_exists('bson_decode')) {
$bsonEncoded = bson_encode($value);
$benchmark->add('bson_decode', function() use (&$bsonEncoded) {
bson_decode($bsonEncoded);
});
}
if (function_exists('msgpack_pack')) {
$msgPack = msgpack_pack($value);
$benchmark->add('msgpack_unpack', function() use (&$msgPack) {
msgpack_unpack($msgPack);
});
}
if (function_exists('igbinary_unserialize')) {
$igbinarySerialized = igbinary_serialize($value);
$benchmark->add('igbinary_unserialize', function() use (&$igbinarySerialized) {
igbinary_unserialize($igbinarySerialized);
});
}
$benchmark->setCount($n);
$benchmark->run();
}
echo "Unserialize Array " . PHP_EOL;
bench([
'test' => 1,
'test2' => true,
'test3' => false,
'test4' => 123456789,
'test5' => 123456789.0,
'test6' => [1,2,3,4,5,6,7,8,9]
]);
$class = new \stdClass();
$class->property1 = true;
$class->property2 = false;
$class->property3 = "test string";
$class->property4 = 1;
$class->property5 = 1.0;
echo PHP_EOL . "Unserialize stdClass " . PHP_EOL;
bench($class);
echo PHP_EOL . "Unserialize Array with 10000 integer(s)" . PHP_EOL;
bench(range(1, 10000), 1000);