forked from krakjoe/jitfu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest2.php
More file actions
73 lines (61 loc) · 1.63 KB
/
test2.php
File metadata and controls
73 lines (61 loc) · 1.63 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
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/**
* This example is based loosely on tutorial from libjit source
*
* http://git.savannah.gnu.org/cgit/libjit.git/tree/tutorial/t2.c
*/
$context = jit_context_create();
jit_context_build_start($context);
$signature = jit_type_create_signature(
JIT_ABI_CDECL, JIT_TYPE_INT, [
JIT_TYPE_INT,
JIT_TYPE_INT
], 1);
$function = jit_function_create($context, $signature);
var_dump($function, $context, $signature);
$x = jit_value_get_param($function, 0);
$y = jit_value_get_param($function, 1);
var_dump($x, $y);
$temp1 = jit_insn_eq($function, $x, $y);
$label1 = jit_insn_branch_if_not($function, $temp1);
jit_insn_return($function, $x);
jit_insn_label($function, $label1);
$temp2 = jit_insn_lt($function, $x, $y);
$label2 = jit_insn_branch_if_not($function, $temp2);
$temp3 = jit_insn_call($function, "gcd", $function, $signature, [
$x, jit_insn_sub($function, $y, $x)
]);
jit_insn_return($function, $temp3);
jit_insn_label($function, $label2);
$temp4 = jit_insn_call($function, "gcd", $function, $signature, [
jit_insn_sub($function, $x, $y), $y
]);
jit_insn_return($function, $temp4);
jit_function_compile($function);
jit_context_build_end($context);
$start = microtime(true);
for ($i=0; $i<10000; $i++) {
jit_function_apply($function, [40, 500], JIT_TYPE_INT);
}
printf("jit: %.3f seconds\n", microtime(true)-$start);
function gcd($x, $y)
{
if($x == $y)
{
return $x;
}
else if($x < $y)
{
return gcd($x, $y - $x);
}
else
{
return gcd($x - $y, $y);
}
}
$start = microtime(true);
for ($i=0; $i<10000; $i++) {
gcd(40, 500);
}
printf("php: %.3f seconds\n", microtime(true)-$start);
?>