Skip to content

Commit 4f166e5

Browse files
committed
Optimization.
1 parent 1735d99 commit 4f166e5

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

src/Views/ArrayIndexListView.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ public function __construct(&$source, array $indexes, ?bool $readonly = null)
5454
*/
5555
public function toArray(): array
5656
{
57-
/** @var Array<T> */
58-
return array_map(fn(int $index) => $this[$index], array_keys($this->indexes));
57+
return array_map(fn (int $index) => $this->source[$index], $this->indexes);
5958
}
6059

6160
/**

tests/unit/ArrayIndexListView/ReadTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,21 @@ public function dataProviderForRead(): array
119119
[[1], [], []],
120120
[[1, 2, 3], [], []],
121121
[[1], [0], [1]],
122+
[[1], [-1], [1]],
122123
[[1], [0, 0], [1, 1]],
124+
[[1], [0, -1], [1, 1]],
123125
[[1], [0, 0, 0], [1, 1, 1]],
124126
[[1, 2], [0], [1]],
125127
[[1, 2], [1], [2]],
128+
[[1, 2], [-1], [2]],
129+
[[1, 2], [-2], [1]],
126130
[[1, 2], [0, 1], [1, 2]],
127131
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 3, 5, 7], [2, 4, 6, 8]],
128132
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [7, 5, 3, 1], [8, 6, 4, 2]],
129133
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 5, 3, 7], [2, 6, 4, 8]],
130134
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 7, 8], [1, 2, 8, 9]],
131135
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 1, 5, 5, 3], [2, 2, 6, 6, 4]],
136+
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [-1, -1, 5, 5, 3], [9, 9, 6, 6, 4]],
132137
];
133138
}
134139
}

tests/unit/Examples/BenchTest.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,39 @@ class BenchTest extends \Codeception\Test\Unit
1010
{
1111
private $n = 10000;
1212

13+
public function testReadIndexListView()
14+
{
15+
$n = $this->n;
16+
$originalArray = range(0, $n);
17+
$indexes = range(0, $n, 2);
18+
19+
$ts = \microtime(true);
20+
$view = ArrayView::toView($originalArray);
21+
$result = $view[$indexes];
22+
$spent = \round(\microtime(true) - $ts, 4);
23+
24+
echo " [testReadIndexListView] SPENT: {$spent} s\n";
25+
ob_flush();
26+
}
27+
28+
public function testReadIndexListPure()
29+
{
30+
$n = $this->n;
31+
$originalArray = range(0, $n);
32+
$indexes = range(0, $n, 2);
33+
$n_2 = \count($indexes);
34+
35+
$ts = \microtime(true);
36+
$result = [];
37+
for ($i = 0; $i < $n_2; $i++) {
38+
$result[$i] = $originalArray[$indexes[$i]];
39+
}
40+
$spent = \round(\microtime(true) - $ts, 4);
41+
42+
echo " [testReadIndexListPure] SPENT: {$spent} s\n";
43+
ob_flush();
44+
}
45+
1346
public function testReadSliceView()
1447
{
1548
$n = $this->n;
@@ -20,7 +53,7 @@ public function testReadSliceView()
2053
$result = $view['::2'];
2154
$spent = \round(\microtime(true) - $ts, 4);
2255

23-
echo "[testReadSliceView] SPENT: {$spent} s\n";
56+
echo " [testReadSliceView] SPENT: {$spent} s\n";
2457
ob_flush();
2558
}
2659

@@ -37,9 +70,10 @@ public function testReadSlicePure()
3770
}
3871
$spent = \round(\microtime(true) - $ts, 4);
3972

40-
echo "[testReadSlicePure] SPENT: {$spent} s\n";
73+
echo " [testReadSlicePure] SPENT: {$spent} s\n";
4174
ob_flush();
4275
}
76+
4377
public function testWriteSliceView()
4478
{
4579
$n = $this->n;
@@ -52,7 +86,7 @@ public function testWriteSliceView()
5286
$view['::2'] = $toWrite;
5387
$spent = \round(\microtime(true) - $ts, 4);
5488

55-
echo "[testWriteSliceView] SPENT: {$spent} s\n";
89+
echo " [testWriteSliceView] SPENT: {$spent} s\n";
5690
ob_flush();
5791
}
5892

@@ -69,7 +103,7 @@ public function testWriteSlicePure()
69103
}
70104
$spent = \round(\microtime(true) - $ts, 4);
71105

72-
echo "[testWriteSlicePure] SPENT: {$spent} s\n";
106+
echo " [testWriteSlicePure] SPENT: {$spent} s\n";
73107
ob_flush();
74108
}
75109
}

0 commit comments

Comments
 (0)