Skip to content
This repository was archived by the owner on Dec 28, 2023. It is now read-only.

Commit 3504ab0

Browse files
committed
Add benchmarks
1 parent 3db42bd commit 3504ab0

File tree

9 files changed

+191
-9
lines changed

9 files changed

+191
-9
lines changed

benchmarks/CollectionBenchmark.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@
1010
trait CollectionBenchmark
1111
{
1212
/**
13-
* @iterations 1000
13+
* @iterations 100
1414
*/
1515
public function arrayProvider()
1616
{
1717
$this->execute(new Collection($this->data, ArrayProvider::getInstance()));
1818
}
1919

2020
/**
21-
* @iterations 1000
21+
* @iterations 100
2222
*/
2323
public function iteratorProvider()
2424
{
2525
$this->execute(new Collection($this->data, IteratorProvider::getInstance()));
2626
}
2727

2828
/**
29-
* @iterations 1000
29+
* @iterations 100
3030
*/
3131
public function generatorProvider()
3232
{

benchmarks/ConcatEvent.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Emonkak\Collection\Benchmarks;
4+
5+
use Athletic\AthleticEvent;
6+
7+
class ReduceEvent extends AthleticEvent
8+
{
9+
use CollectionBenchmark;
10+
11+
public function setUp()
12+
{
13+
$this->data = range(0, 1000);
14+
}
15+
16+
/**
17+
* @iterations 100
18+
*/
19+
public function arrayImpl()
20+
{
21+
foreach (array_merge($this->data, $this->data) as $x);
22+
}
23+
24+
protected function execute($xs)
25+
{
26+
foreach ($xs->concat($this->data) as $x);
27+
}
28+
}

benchmarks/FilterEvent.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,24 @@
33
namespace Emonkak\Collection\Benchmarks;
44

55
use Athletic\AthleticEvent;
6+
use Emonkak\Collection\Collection;
67

78
class FilterEvent extends AthleticEvent
89
{
910
use CollectionBenchmark;
1011

1112
public function setUp()
1213
{
13-
$this->data = range(0, 100);
14+
$this->data = range(0, 1000);
1415
$this->predicate = function($x) {
1516
return $x % 2 === 0;
1617
};
1718
}
1819

1920
/**
20-
* @iterations 1000
21+
* @iterations 100
2122
*/
22-
public function array_filter()
23+
public function arrayImpl()
2324
{
2425
foreach (array_filter($this->data, $this->predicate) as $x);
2526
}

benchmarks/GroupByEvent.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Emonkak\Collection\Benchmarks;
4+
5+
use Athletic\AthleticEvent;
6+
7+
class GroupByEvent extends AthleticEvent
8+
{
9+
use CollectionBenchmark;
10+
11+
public function setUp()
12+
{
13+
$this->data = range(0, 1000);
14+
$this->selector = function($x) {
15+
return $x % 10;
16+
};
17+
}
18+
19+
protected function execute($xs)
20+
{
21+
foreach ($xs->groupBy($this->selector) as $k => $x);
22+
}
23+
}

benchmarks/JoinEvent.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Emonkak\Collection\Benchmarks;
4+
5+
use Athletic\AthleticEvent;
6+
7+
class JoinEvent extends AthleticEvent
8+
{
9+
use CollectionBenchmark;
10+
11+
public function setUp()
12+
{
13+
$this->data = range(0, 1000);
14+
}
15+
16+
/**
17+
* @iterations 100
18+
*/
19+
public function arrayImpl()
20+
{
21+
implode(',', $this->data);
22+
}
23+
24+
protected function execute($xs)
25+
{
26+
$xs->join(',');
27+
}
28+
}

benchmarks/MapEvent.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ class MapEvent extends AthleticEvent
1010

1111
public function setUp()
1212
{
13-
$this->data = range(0, 100);
13+
$this->data = range(0, 1000);
1414
$this->selector = function($x) {
1515
return $x * 2;
1616
};
1717
}
1818

1919
/**
20-
* @iterations 1000
20+
* @iterations 100
2121
*/
22-
public function array_map()
22+
public function arrayImpl()
2323
{
2424
foreach (array_map($this->selector, $this->data) as $x);
2525
}

benchmarks/MethodChainEvent.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Emonkak\Collection\Benchmarks;
4+
5+
use Athletic\AthleticEvent;
6+
use Emonkak\Collection\Collection;
7+
8+
class MethodChainEvent extends AthleticEvent
9+
{
10+
use CollectionBenchmark;
11+
12+
public function setUp()
13+
{
14+
$this->data = range(0, 1000);
15+
}
16+
17+
/**
18+
* @iterations 100
19+
*/
20+
public function arrayImpl()
21+
{
22+
$result = $this->data;
23+
$result = array_filter($result, function($x) { return $x % 2 === 0; });
24+
$result = array_map(function($x) { return $x * 2; }, $result);
25+
$result = array_merge($result, $this->data);
26+
for ($i = 0, $l = count($result); $i < $l; $i++) {
27+
$result[$i] = [$result[$i], $i];
28+
}
29+
$result = array_slice($result, 0, 100);
30+
foreach ($result as $x);
31+
}
32+
33+
protected function execute($xs)
34+
{
35+
$result = $xs
36+
->filter(function($x) { return $x % 2 === 0; })
37+
->map(function($x) { return $x * 2; })
38+
->concat($this->data)
39+
->zip(Collection::range(0, INF))
40+
->take(100);
41+
foreach ($result as $x);
42+
}
43+
}

benchmarks/ReduceEvent.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Emonkak\Collection\Benchmarks;
4+
5+
use Athletic\AthleticEvent;
6+
7+
class ReduceEvent extends AthleticEvent
8+
{
9+
use CollectionBenchmark;
10+
11+
public function setUp()
12+
{
13+
$this->data = range(0, 1000);
14+
$this->f = function($acc, $x) {
15+
return $acc + $x;
16+
};
17+
}
18+
19+
/**
20+
* @iterations 100
21+
*/
22+
public function arrayImpl()
23+
{
24+
array_reduce($this->data, $this->f, 0);
25+
}
26+
27+
protected function execute($xs)
28+
{
29+
$xs->reduce($this->f, 0);
30+
}
31+
}

benchmarks/UniqEvent.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Emonkak\Collection\Benchmarks;
4+
5+
use Athletic\AthleticEvent;
6+
7+
class UniqEvent extends AthleticEvent
8+
{
9+
use CollectionBenchmark;
10+
11+
public function setUp()
12+
{
13+
$this->data = array_merge(range(0, 250), range(0, 250), range(0, 250), range(0, 250));
14+
}
15+
16+
/**
17+
* @iterations 100
18+
*/
19+
public function arrayImpl()
20+
{
21+
foreach (array_unique($this->data) as $x);
22+
}
23+
24+
protected function execute($xs)
25+
{
26+
foreach ($xs->uniq() as $x);
27+
}
28+
}

0 commit comments

Comments
 (0)