-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathbench.php
More file actions
61 lines (52 loc) · 1.45 KB
/
bench.php
File metadata and controls
61 lines (52 loc) · 1.45 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
50
51
52
53
54
55
56
57
58
59
60
61
<?php
// Benchmark: measures interpreter performance
$start = microtime(true);
// Test 1: Variable access and arithmetic (heavy on variable lookups)
$sum = 0;
for ($i = 0; $i < 100000; $i++) {
$sum = $sum + $i;
}
$t1 = microtime(true) - $start;
echo "Test 1 (variable + arithmetic): " . round($t1 * 1000, 2) . " ms\n";
// Test 2: Function calls (heavy on context creation)
function add($a, $b) {
return $a + $b;
}
$start = microtime(true);
$sum = 0;
for ($i = 0; $i < 50000; $i++) {
$sum = add($sum, $i);
}
$t2 = microtime(true) - $start;
echo "Test 2 (function calls): " . round($t2 * 1000, 2) . " ms\n";
// Test 3: Array operations
$start = microtime(true);
$arr = [];
for ($i = 0; $i < 10000; $i++) {
$arr[] = $i;
}
$sum = 0;
foreach ($arr as $v) {
$sum = $sum + $v;
}
$t3 = microtime(true) - $start;
echo "Test 3 (array operations): " . round($t3 * 1000, 2) . " ms\n";
// Test 4: String concatenation
$start = microtime(true);
$str = "";
for ($i = 0; $i < 10000; $i++) {
$str = $str . "x";
}
$t4 = microtime(true) - $start;
echo "Test 4 (string concat): " . round($t4 * 1000, 2) . " ms\n";
// Test 5: Nested function calls
function fib($n) {
if ($n <= 1) return $n;
return fib($n - 1) + fib($n - 2);
}
$start = microtime(true);
$result = fib(20);
$t5 = microtime(true) - $start;
echo "Test 5 (recursive fib(20)): " . round($t5 * 1000, 2) . " ms\n";
$total = $t1 + $t2 + $t3 + $t4 + $t5;
echo "\nTotal: " . round($total * 1000, 2) . " ms\n";