Skip to content

Commit a02a0ed

Browse files
committed
Optimization.
1 parent 5b74847 commit a02a0ed

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/Views/ArrayView.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,11 +406,10 @@ public function set($newValues): self
406406
throw new SizeError("Length of values array not equal to view length ({$dataSize} != {$thisSize}).");
407407
}
408408

409-
$newValuesView = ArrayView::toView($newValues);
410-
411409
$size = \count($this);
410+
412411
for ($i = 0; $i < $size; $i++) {
413-
$this[$i] = $newValuesView[$i];
412+
$this[$i] = $newValues[$i];
414413
}
415414

416415
return $this;

tests/unit/Examples/BenchTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Smoren\ArrayView\Tests\Unit\Examples;
6+
7+
use Smoren\ArrayView\Views\ArrayView;
8+
9+
class BenchTest extends \Codeception\Test\Unit
10+
{
11+
public function testWriteSliceView()
12+
{
13+
$n = 10000;
14+
$n_2 = intval($n / 2);
15+
$originalArray = range(0, $n);
16+
$toWrite = range(0, $n_2);
17+
18+
$ts = \microtime(true);
19+
$view = ArrayView::toView($originalArray);
20+
$view['::2'] = $toWrite;
21+
$spent = \round(\microtime(true) - $ts, 4);
22+
23+
echo "SPENT: {$spent} s\n";
24+
ob_flush();
25+
}
26+
27+
public function testWriteSlicePure()
28+
{
29+
$n = 10000;
30+
$n_2 = intval($n / 2);
31+
$originalArray = range(0, $n);
32+
$toWrite = range(0, $n_2);
33+
34+
$ts = \microtime(true);
35+
for ($i = 0; $i < $n_2; $i++) {
36+
$originalArray[$i * 2] = $toWrite[$i];
37+
}
38+
$spent = \round(\microtime(true) - $ts, 4);
39+
40+
echo "SPENT: {$spent} s\n";
41+
ob_flush();
42+
}
43+
}

0 commit comments

Comments
 (0)