Skip to content

Commit

Permalink
Optimization.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Mar 16, 2024
1 parent 5b74847 commit a02a0ed
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/Views/ArrayView.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,10 @@ public function set($newValues): self
throw new SizeError("Length of values array not equal to view length ({$dataSize} != {$thisSize}).");
}

$newValuesView = ArrayView::toView($newValues);

$size = \count($this);

for ($i = 0; $i < $size; $i++) {
$this[$i] = $newValuesView[$i];
$this[$i] = $newValues[$i];
}

return $this;
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/Examples/BenchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Smoren\ArrayView\Tests\Unit\Examples;

use Smoren\ArrayView\Views\ArrayView;

class BenchTest extends \Codeception\Test\Unit
{
public function testWriteSliceView()
{
$n = 10000;
$n_2 = intval($n / 2);
$originalArray = range(0, $n);
$toWrite = range(0, $n_2);

$ts = \microtime(true);
$view = ArrayView::toView($originalArray);
$view['::2'] = $toWrite;
$spent = \round(\microtime(true) - $ts, 4);

echo "SPENT: {$spent} s\n";
ob_flush();
}

public function testWriteSlicePure()
{
$n = 10000;
$n_2 = intval($n / 2);
$originalArray = range(0, $n);
$toWrite = range(0, $n_2);

$ts = \microtime(true);
for ($i = 0; $i < $n_2; $i++) {
$originalArray[$i * 2] = $toWrite[$i];
}
$spent = \round(\microtime(true) - $ts, 4);

echo "SPENT: {$spent} s\n";
ob_flush();
}
}

0 comments on commit a02a0ed

Please sign in to comment.