-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bench-write.php
84 lines (69 loc) · 2.06 KB
/
bench-write.php
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
use LeKoala\SpreadCompat\Csv\League;
use LeKoala\SpreadCompat\Csv\Native;
use LeKoala\SpreadCompat\Csv\OpenSpout;
use LeKoala\SpreadCompat\Csv\PhpSpreadsheet;
use LeKoala\SpreadCompat\SpreadCompat;
use LeKoala\SpreadCompat\Xlsx\Native as XlsxNative;
use LeKoala\SpreadCompat\Xlsx\PhpSpreadsheet as XlsxPhpSpreadsheet;
use LeKoala\SpreadCompat\Xlsx\OpenSpout as XlsxOpenSpout;
use LeKoala\SpreadCompat\Xlsx\Simple;
require './vendor/autoload.php';
$largeCsv = SpreadCompat::getTempFilename();
$largeXlsx = SpreadCompat::getTempFilename();
$genData = [];
foreach (range(1, 2500) as $i) {
$genData[] = [$i, "fname $i", "sname $i", "[email protected]"];
}
$csv = [
League::class,
OpenSpout::class,
Native::class,
PhpSpreadsheet::class
];
$xlsx = [
Simple::class,
XlsxOpenSpout::class,
XlsxPhpSpreadsheet::class,
XlsxNative::class,
];
$reps = 5;
$times = [];
foreach ($csv as $cl) {
foreach (range(1, $reps) as $i) {
/** @var \LeKoala\SpreadCompat\Csv\CsvAdapter $inst */
$inst = new ($cl);
$st = microtime(true);
$inst->writeFile($genData, $largeCsv);
$et = microtime(true);
$diff = $et - $st;
$times['csv'][$cl][] = $diff;
}
}
foreach ($xlsx as $cl) {
foreach (range(1, $reps) as $i) {
/** @var \LeKoala\SpreadCompat\Xlsx\XlsxAdapter $inst */
$inst = new ($cl);
try {
$st = microtime(true);
$inst->writeFile($genData, $largeXlsx);
$et = microtime(true);
$diff = $et - $st;
$times['xlsx'][$cl][] = $diff;
} catch (Exception $e) {
}
}
}
foreach ($times as $format => $dataFormat) {
echo "Results for $format" . PHP_EOL;
$results = [];
foreach ($dataFormat as $class => $times) {
$averageTime = round(array_sum($times) / count($times), 4);
$results[$class] = $averageTime;
}
uasort($results, fn ($a, $b) => $a <=> $b);
foreach ($results as $class => $averageTime) {
echo "$class : " . $averageTime . PHP_EOL;
}
echo PHP_EOL;
}