-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench.php
More file actions
49 lines (40 loc) · 1.77 KB
/
Copy pathbench.php
File metadata and controls
49 lines (40 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/*
|--------------------------------------------------------------------------
| Benchmark
|--------------------------------------------------------------------------
|
| Run with "composer bench". Not part of the test suite: wall-clock numbers
| are too noisy on CI runners to assert on. The test suite guards against
| the regressions this catches by counting work instead of timing it.
|
| Pass a row count to change the fixture size: composer bench -- 5000000
|
*/
require __DIR__.'/vendor/autoload.php';
use Heller\SimpleCsv\Csv;
$rows = (int) ($argv[1] ?? 1_000_000);
$file = sys_get_temp_dir().'/simple-csv-bench.csv';
$handle = fopen($file, 'w');
fwrite($handle, "Foo,Bar,Baz\n");
for ($i = 0; $i < $rows - 1; $i++) {
fwrite($handle, "Foo$i,Bar$i,Baz$i\n");
}
fclose($handle);
printf("%s rows, %.0f MB, PHP %s\n\n", number_format($rows), filesize($file) / 1048576, PHP_VERSION);
$cases = [
'each()' => fn () => Csv::read($file)->each(fn ($row) => null),
'each() + mapToHeaders()' => fn () => Csv::read($file)->mapToHeaders()->each(fn ($row) => null),
'each() + mapToObject()' => fn () => Csv::read($file)->mapToObject()->each(fn ($row) => null),
'each() + skipColumns()' => fn () => Csv::read($file)->mapToHeaders()->skipColumns([2])->each(fn ($row) => null),
'each() + filter()' => fn () => Csv::read($file)->mapToHeaders()->filter(fn ($row) => $row['Foo'] === 'Foo1')->each(fn ($row) => null),
'count()' => fn () => Csv::read($file)->count(),
'toArray()' => fn () => Csv::read($file)->mapToHeaders()->toArray(),
];
foreach ($cases as $label => $case) {
gc_collect_cycles();
$start = microtime(true);
$case();
printf(" %-26s %6.2fs %4.0f MB peak\n", $label, microtime(true) - $start, memory_get_peak_usage(true) / 1048576);
}
unlink($file);