Skip to content

Commit 901903c

Browse files
author
gabbydgab
committed
added benchmarks for array merge, filter, validator and interator-to-array
1 parent 07f80c3 commit 901903c

File tree

4 files changed

+406
-0
lines changed

4 files changed

+406
-0
lines changed

benchmark/ArrayFilterBench.php

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/**
4+
* Zend Framework (http://framework.zend.com/)
5+
*
6+
* @link http://github.com/zendframework/zend-array-utility for the canonical source repository
7+
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
8+
* @license http://framework.zend.com/license/new-bsd New BSD License
9+
*/
10+
11+
namespace ZendBench\ArrayUtils;
12+
13+
use Zend\ArrayUtils\ArrayUtils;
14+
15+
final class ArrayFilterBench
16+
{
17+
public function benchNoFilter()
18+
{
19+
$array = ['foo' => 'bar', 'fiz' => 'buz'];
20+
$callback = function ($value) {
21+
if ($value == 'bar') {
22+
return false;
23+
}
24+
return true;
25+
};
26+
27+
ArrayUtils::filter($array, $callback);
28+
}
29+
30+
public function benchUseKeyFiltering()
31+
{
32+
$array = ['foo' => 'bar', 'fiz' => 'buz'];
33+
$callback = function ($key) {
34+
if ($key == 'foo') {
35+
return false;
36+
}
37+
return true;
38+
};
39+
$flag = ArrayUtils::ARRAY_FILTER_USE_KEY;
40+
41+
ArrayUtils::filter($array, $callback, $flag);
42+
}
43+
44+
public function benchFilterBoth()
45+
{
46+
$array = ['foo' => 'bar', 'fiz' => 'buz'];
47+
$callback = function ($value, $key) {
48+
if ($value == 'buz') {
49+
return false;
50+
}
51+
52+
if ($key == 'foo') {
53+
return false;
54+
}
55+
56+
return true;
57+
};
58+
$flag = ArrayUtils::ARRAY_FILTER_USE_BOTH;
59+
60+
ArrayUtils::filter($array, $callback, $flag);
61+
}
62+
}

benchmark/ArrayMergeBench.php

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
3+
/**
4+
* Zend Framework (http://framework.zend.com/)
5+
*
6+
* @link http://github.com/zendframework/zend-array-utility for the canonical source repository
7+
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
8+
* @license http://framework.zend.com/license/new-bsd New BSD License
9+
*/
10+
11+
namespace ZendBench\ArrayUtils;
12+
13+
use Zend\ArrayUtils\ArrayUtils;
14+
15+
final class ArrayMergeBench
16+
{
17+
public function benchMergingIntegerAndStringKeys()
18+
{
19+
$a = [
20+
'foo',
21+
3 => 'bar',
22+
'baz' => 'baz',
23+
4 => [
24+
'a',
25+
1 => 'b',
26+
'c',
27+
],
28+
];
29+
30+
$b = [
31+
'baz',
32+
4 => [
33+
'd' => 'd',
34+
],
35+
];
36+
37+
ArrayUtils::merge($a, $b);
38+
}
39+
40+
public function benchMergingIntegerAndStringWithPreservedNumericKeys()
41+
{
42+
$a = [
43+
'foo',
44+
3 => 'bar',
45+
'baz' => 'baz',
46+
4 => [
47+
'a',
48+
1 => 'b',
49+
'c',
50+
],
51+
];
52+
53+
$b = [
54+
'baz',
55+
4 => [
56+
'd' => 'd',
57+
],
58+
];
59+
60+
ArrayUtils::merge($a, $b, true);
61+
}
62+
63+
public function benchRecursiveMerge()
64+
{
65+
$a = [
66+
'foo' => [
67+
'baz'
68+
]
69+
];
70+
$b = [
71+
'foo' => [
72+
'baz'
73+
]
74+
];
75+
76+
ArrayUtils::merge($a, $b);
77+
}
78+
79+
public function benchReplaceStringKeys()
80+
{
81+
$a = [
82+
'foo' => 'bar',
83+
'bar' => []
84+
];
85+
$b = [
86+
'foo' => 'baz',
87+
'bar' => 'bat'
88+
];
89+
90+
ArrayUtils::merge($a, $b);
91+
}
92+
93+
public function benchMergeWithNullArray()
94+
{
95+
$a = [
96+
'foo' => null,
97+
null => 'rod',
98+
'cat' => 'bar',
99+
'god' => 'rad'
100+
];
101+
$b = [
102+
'foo' => 'baz',
103+
null => 'zad',
104+
'god' => null
105+
];
106+
107+
ArrayUtils::merge($a, $b);
108+
}
109+
110+
public function benchMergeReplaceKeys()
111+
{
112+
$a = [
113+
'car' => [
114+
'boo' => 'foo',
115+
'doo' => 'moo',
116+
],
117+
];
118+
$b = [
119+
'car' => new \Zend\ArrayUtils\ArrayUtils\MergeReplaceKey([
120+
'met' => 'bet',
121+
]),
122+
'new' => new \Zend\ArrayUtils\ArrayUtils\MergeReplaceKey([
123+
'foo' => 'get',
124+
]),
125+
];
126+
127+
ArrayUtils::merge($a, $b);
128+
}
129+
130+
public function benchRemovingKeys()
131+
{
132+
$a = [
133+
'foo' => 'bar',
134+
'bar' => 'bat'
135+
];
136+
$b = [
137+
'foo' => new \Zend\ArrayUtils\ArrayUtils\MergeRemoveKey(),
138+
'baz' => new \Zend\ArrayUtils\ArrayUtils\MergeRemoveKey(),
139+
];
140+
141+
ArrayUtils::merge($a, $b);
142+
}
143+
}

benchmark/ArrayValidatorBench.php

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
3+
/**
4+
* Zend Framework (http://framework.zend.com/)
5+
*
6+
* @link http://github.com/zendframework/zend-array-utility for the canonical source repository
7+
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
8+
* @license http://framework.zend.com/license/new-bsd New BSD License
9+
*/
10+
11+
namespace ZendBench\ArrayUtils;
12+
13+
use Zend\ArrayUtils\ArrayUtils;
14+
15+
final class ArrayValidatorBench
16+
{
17+
public function benchStringKeyValidation()
18+
{
19+
$test = [
20+
[[
21+
'foo' => 'bar',
22+
]],
23+
[[
24+
'bar',
25+
'foo' => 'bar',
26+
'baz',
27+
]],
28+
];
29+
30+
ArrayUtils::hasStringKeys($test);
31+
}
32+
33+
public function benchIntegerKeyValidation()
34+
{
35+
return [
36+
[[
37+
'foo',
38+
'bar,'
39+
]],
40+
[[
41+
100 => 'foo',
42+
200 => 'bar'
43+
]],
44+
[[
45+
-100 => 'foo',
46+
0 => 'bar',
47+
100 => 'baz'
48+
]],
49+
[[
50+
'foo',
51+
'bar',
52+
1000 => 'baz'
53+
]],
54+
];
55+
56+
ArrayUtils::hasIntegerKeys($test);
57+
}
58+
59+
public function benchValidArraysWithNumericKeys()
60+
{
61+
$test = [
62+
[[
63+
'foo',
64+
'bar'
65+
]],
66+
[[
67+
'0' => 'foo',
68+
'1' => 'bar',
69+
]],
70+
[[
71+
'bar',
72+
'1' => 'bar',
73+
3 => 'baz'
74+
]],
75+
[[
76+
-10000 => null,
77+
'-10000' => null,
78+
]],
79+
[[
80+
'-00000.00009' => 'foo'
81+
]],
82+
[[
83+
1 => 0
84+
]],
85+
];
86+
87+
ArrayUtils::hasNumericKeys($test);
88+
}
89+
90+
public function benchArrayList()
91+
{
92+
$test = [
93+
[[null]],
94+
[[true]],
95+
[[false]],
96+
[[0]],
97+
[[-0.9999]],
98+
[['string']],
99+
[[new \stdClass()]],
100+
[[
101+
0 => 'foo',
102+
1 => 'bar',
103+
2 => false,
104+
3 => null,
105+
4 => [],
106+
5 => new \stdClass()
107+
]]
108+
];
109+
110+
ArrayUtils::isList($test);
111+
}
112+
113+
public function benchHashKeyValidation()
114+
{
115+
$test = [
116+
[[
117+
'foo' => 'bar'
118+
]],
119+
[[
120+
'15',
121+
'foo' => 'bar',
122+
'baz' => ['baz']
123+
]],
124+
[[
125+
0 => false,
126+
2 => null
127+
]],
128+
[[
129+
-100 => 'foo',
130+
100 => 'bar'
131+
]],
132+
[[
133+
1 => 0
134+
]],
135+
];
136+
137+
ArrayUtils::isHashTable($test);
138+
}
139+
}

0 commit comments

Comments
 (0)