-
Notifications
You must be signed in to change notification settings - Fork 58
/
run-benchmarks.php
110 lines (88 loc) · 4.27 KB
/
run-benchmarks.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
const PATTERNS_COUNT = 3;
const RUN_TIMES = 10;
const BUILDS = [
'C PCRE2' => 'gcc -O3 -DNDEBUG c/benchmark.c -I/usr/local/include/ -lpcre2-8 -o c/bin/benchmark',
'Crystal' => 'crystal build crystal/benchmark.cr --release -o crystal/bin/benchmark',
'C++ STL' => 'g++ -std=c++11 -O3 cpp/benchmark.cpp -o cpp/bin/benchmark-stl',
'C++ Boost' => 'g++ -std=c++11 -O3 cpp/benchmark.cpp -DUSE_BOOST -lboost_regex -o cpp/bin/benchmark-boost',
'C++ SRELL' => 'g++ -std=c++11 -O3 cpp/benchmark.cpp -DUSE_SRELL -o cpp/bin/benchmark-srell',
'C# Mono' => 'mcs csharp/Benchmark.cs -out:csharp/bin-mono/benchmark.exe -debug- -optimize',
'C# .Net Core' => 'dotnet build csharp/benchmark.csproj -c Release',
'D dmd' => 'dmd -O -release -inline -of=d/bin/benchmark d/benchmark.d',
'D ldc' => 'ldc2 -O3 -release -of=d/bin/benchmark-ldc d/benchmark.d',
'Dart Native' => 'mkdir -p /var/regex/dart/bin && dart2native dart/benchmark.dart -o dart/bin/benchmark',
'Go' => 'go env -w GO111MODULE=auto && go build -ldflags "-s -w" -o go/bin/benchmark ./go',
'Java' => 'javac java/Benchmark.java',
'Kotlin' => 'kotlinc kotlin/benchmark.kt -include-runtime -d kotlin/benchmark.jar',
'Nim' => 'nim c -d:release --opt:speed --verbosity:0 -o:nim/bin/benchmark nim/benchmark.nim',
'Nim Regex' => 'nim c -d:release --opt:speed --verbosity:0 -o:nim/bin/benchmark_regex nim/benchmark_regex.nim',
'Rust' => 'cargo build --quiet --release --manifest-path=rust/Cargo.toml',
];
const COMMANDS = [
'C PCRE2' => 'c/bin/benchmark',
'Crystal' => 'crystal/bin/benchmark',
'C++ STL' => 'cpp/bin/benchmark-stl',
'C++ Boost' => 'cpp/bin/benchmark-boost',
'C++ SRELL' => 'cpp/bin/benchmark-srell',
'C# Mono' => 'mono -O=all csharp/bin-mono/benchmark.exe',
'C# .Net Core' => 'dotnet csharp/bin/Release/net5.0/benchmark.dll',
'D dmd' => 'd/bin/benchmark',
'D ldc' => 'd/bin/benchmark-ldc',
'Dart' => 'dart dart/benchmark.dart',
'Dart Native' => 'dart/bin/benchmark',
'Go' => 'go/bin/benchmark',
'Java' => 'java -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC -classpath java Benchmark',
'Javascript' => 'node javascript/benchmark.js',
'Julia' => 'julia julia/benchmark.jl',
'Kotlin' => 'kotlin kotlin/benchmark.jar',
'Nim' => 'nim/bin/benchmark',
'Nim Regex' => 'nim/bin/benchmark_regex',
'Perl' => 'perl perl/benchmark.pl',
'PHP' => 'php php/benchmark.php',
'Python 2' => 'python2.7 python/benchmark.py',
'Python 3' => 'python3.6 python/benchmark.py',
'Python PyPy2' => 'pypy2 python/benchmark.py',
'Python PyPy3' => 'pypy3 python/benchmark.py',
'Ruby' => 'ruby ruby/benchmark.rb',
'Rust' => 'rust/target/release/benchmark',
];
echo '- Build' . PHP_EOL;
foreach (BUILDS as $language => $buildCmd) {
shell_exec($buildCmd);
echo $language . ' built.' . PHP_EOL;
}
echo PHP_EOL . '- Run' . PHP_EOL;
$results = [];
foreach (COMMANDS as $language => $command) {
echo $language . ' running.';
$currentResults = [];
for ($i = 0; $i < RUN_TIMES; $i++) {
$out = shell_exec($command . ' input-text.txt');
preg_match_all('/^\d+\.\d+/m', $out, $matches);
if (sizeof($matches[0]) === 0) {
break;
}
for ($j = 0; $j < PATTERNS_COUNT; $j++) {
$currentResults[$j][] = $matches[0][$j];
}
echo '.';
}
if (sizeof($currentResults) !== 0) {
for ($i = 0; $i < PATTERNS_COUNT; $i++) {
$results[$language][] = array_sum($currentResults[$i]) / count($currentResults[$i]);
}
$results[$language][PATTERNS_COUNT] = array_sum($results[$language]);
}
echo $language . ' ran.' . PHP_EOL;
}
echo PHP_EOL . '- Results' . PHP_EOL;
uasort($results, function ($a, $b) {
return $a[PATTERNS_COUNT] < $b[PATTERNS_COUNT] ? -1 : 1;
});
$results = array_walk($results, function ($result, $language) {
$result = array_map(function ($time) {
return number_format($time, 2, '.', '');
}, $result);
echo '**' . $language . '** | ' . implode(' | ', $result) . PHP_EOL;
});