Skip to content

Commit 6c970be

Browse files
committed
finalize
1 parent 4a57826 commit 6c970be

File tree

2 files changed

+44
-24
lines changed

2 files changed

+44
-24
lines changed

README.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
# php-ds-array-benchmark
22

3-
A synthetic benchmark comparison between native Array, SplFixedArray and Deque/Vector from php-ds.
3+
A php8.2 benchmark comparison between native **Array**, **SplFixedArray** and **Vector** from php-ds (data-structures extensions).
44

55
# Usage
66

7-
./vendor/bin/phpbench run tests/Benchmark --report=default
8-
./vendor/bin/phpbench run tests/Benchmark --report=aggregate
7+
./vendor/bin/phpbench run tests/Benchmark --report=aggregate --retry-threshold=5
98

9+
# Result
1010

11+
### Without json_encode
12+
13+
| benchmark | subject | set | revs | its | mem_peak | mode | rstdev |
14+
|------------|--------------------|-----|------|-----|----------|-------------|--------|
15+
| ArrayBench | benchArray | | 10 | 3 | 1.126gb | 492.811ms | ±1.31% |
16+
| ArrayBench | benchSplFixedArray | | 10 | 3 | 1.087gb | 537.613ms | ±2.15% |
17+
| ArrayBench | benchDsVector | | 10 | 3 | 1.084gb | 1,021.727ms | ±0.19% |
18+
19+
### With json_encode
20+
21+
| benchmark | subject | set | revs | its | mem_peak | mode | rstdev |
22+
|------------|--------------------|-----|------|-----|----------|-------------|--------|
23+
| ArrayBench | benchArray | | 10 | 3 | 1.405gb | 1,565.699ms | ±1.01% |
24+
| ArrayBench | benchSplFixedArray | | 10 | 3 | 1.600gb | 2,087.920ms | ±0.71% |
25+
| ArrayBench | benchDsVector | | 10 | 3 | 1.388gb | 2,137.282ms | ±0.61% |
26+
27+
# Conclusion
28+
29+
- **Native array is the best data structure for lists, and outperforms the other methods in both time complexity and overall resource usage.**
30+
- SplFixedArray is simply garbage for php8. It has no value over array in any way.
31+
- DataStructures extension was once a great improvement for php7, but so much anymore, at least for simple lists. Certain things can be still much more efficient with [php-ds](https://medium.com/@rtheunissen/efficient-data-structures-for-php-7-9dda7af674cd).

tests/Benchmark/ArrayBench.php

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
use PhpBench\Benchmark\Metadata\Annotations\Revs;
99
use PhpBench\Benchmark\Metadata\Annotations\OutputTimeUnit;
1010
use SplFixedArray;
11-
use Ds\Deque;
11+
use Ds\Vector;
1212

1313
/**
1414
* @BeforeMethods({"init"})
1515
* @AfterMethods({"tearDown"})
16-
* @Iterations(5)
17-
* @Revs(50)
16+
* @Iterations(3)
17+
* @Revs(10)
1818
* @OutputTimeUnit("milliseconds", precision=3)
1919
*/
2020
class ArrayBench
@@ -25,7 +25,8 @@ class ArrayBench
2525

2626
public function init()
2727
{
28-
$this->data = require_once("data/sample_data.php");
28+
$data = require("data/sample_data.php");
29+
$this->data = array_merge($data, $data, $data, $data, $data, $data, $data, $data);
2930
$this->colCount = count($this->data[0]);
3031
$this->count = count($this->data);
3132
}
@@ -39,11 +40,9 @@ public function benchArray()
3940
{
4041
$data = [];
4142
foreach ($this->data as $values) {
42-
$row = [
43-
"cells" => []
44-
];
43+
$cells = [];
4544
foreach ($values as $col => $value) {
46-
$row["cells"][] = [
45+
$cells[] = [
4746
"colspan" => 2,
4847
"rowspan" => 4,
4948
"column" => $col,
@@ -54,22 +53,21 @@ public function benchArray()
5453
],
5554
];
5655
}
57-
$data[] = $row;
56+
$data[] = $cells;
5857
}
5958

59+
//return $data;
6060
return json_encode($data);
6161
}
6262

6363
public function benchSplFixedArray()
6464
{
6565
$data = new SplFixedArray($this->count);
6666
foreach ($this->data as $key => $values) {
67-
$row = [
68-
"cells" => new SplFixedArray($this->colCount)
69-
];
67+
$cells = new SplFixedArray($this->colCount);
7068
$i = 0;
7169
foreach ($values as $col => $value) {
72-
$row["cells"][$i++] = [
70+
$cells[$i++] = [
7371
"colspan" => 2,
7472
"rowspan" => 4,
7573
"column" => $col,
@@ -80,23 +78,23 @@ public function benchSplFixedArray()
8078
],
8179
];
8280
}
83-
$data[$key] = $row;
81+
$data[$key] = $cells;
8482
}
8583

84+
//return $data;
8685
return json_encode($data);
8786
}
8887

8988
public function benchDsVector()
9089
{
91-
$data = new Deque();
90+
$data = new Vector();
9291
$data->allocate($this->count);
9392
foreach ($this->data as $values) {
94-
$row = [
95-
"cells" => new Deque()
96-
];
97-
$row["cells"]->allocate($this->colCount);
93+
$cells = new Vector();
94+
$cells->allocate($this->colCount);
95+
9896
foreach ($values as $col => $value) {
99-
$row["cells"]->push([
97+
$cells->push([
10098
"colspan" => 2,
10199
"rowspan" => 4,
102100
"column" => $col,
@@ -107,9 +105,10 @@ public function benchDsVector()
107105
],
108106
]);
109107
}
110-
$data->push($row);
108+
$data->push($cells);
111109
}
112110

111+
//return $data;
113112
return json_encode($data);
114113
}
115114

0 commit comments

Comments
 (0)