-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-ffi.php
63 lines (48 loc) · 1.46 KB
/
test-ffi.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
<?php
// 一个PHP的fib函数
function fib($n) {
if ($n === 1 || $n === 2) {
return 1;
} else {
return fib($n-1) + fib($n-2);
}
}
// 这里测试PHP下的Fib函数
$time_start = microtime(true);
for ($i=0; $i < 1000000; $i++) {
$v = fib(12);
}
echo '[PHP]fib执行时间:' . (microtime(true) - $time_start).PHP_EOL;
// 下面则是测试Rust的扩展,我们采用debug、release两种模式
$libExtension = (PHP_OS_FAMILY == "Darwin" ? 'dylib' : 'so');
// debug模式
$ffiDebug = FFI::cdef(
"int32_t fib(int32_t n);",
"r2p-fib/target/debug/libr2pfib.$libExtension");
$time_start = microtime(true);
for ($i=0; $i < 10000000; $i++) {
$v = $ffiDebug->fib(12);
}
echo '[Rust]Debug执行时间:' . (microtime(true) - $time_start).PHP_EOL;
// release模式
$ffiRelease = FFI::cdef(
"int32_t fib(int32_t n);",
"r2p-fib/target/release/libr2pfib.$libExtension");
$time_start = microtime(true);
for ($i=0; $i < 10000000; $i++) {
$v = $ffiRelease->fib(12);
}
echo '[Rust]Release执行时间:' . (microtime(true) - $time_start).PHP_EOL;
// 这里我们采用Rust编写的一个PHP扩展
$time_start = microtime(true);
for ($i=0; $i < 10000000; $i++) {
$v = rust_fib(12);
}
echo '[Rust]Ext执行时间:' . (microtime(true) - $time_start).PHP_EOL;
// 这里我们采用C编写的一个PHP扩展
$time_start = microtime(true);
for ($i=0; $i < 10000000; $i++) {
$v = c_fib(12);
}
echo '[C]Ext执行时间:' . (microtime(true) - $time_start).PHP_EOL;
?>