Skip to content

Commit 8fc9313

Browse files
author
llgoer
committed
增加了一个C的测试
1 parent 002c08e commit 8fc9313

File tree

3 files changed

+81
-6
lines changed

3 files changed

+81
-6
lines changed

docs/php-ext.md

+39-4
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,40 @@ PHP_FUNCTION(rust_fib)
124124
```
125125
这里我们只是贴出核心的代码部分。
126126

127+
为了更好的测试,我们同时编写了一个C的版本
128+
129+
```
130+
// 这里直接编写一个C的fib调用
131+
int fibinC(int n)
132+
{
133+
if (n <= 1)
134+
return n;
135+
return fibinC(n - 1) + fibinC(n - 2);
136+
}
137+
/* {{{ int c_fib( [ int $var ] )
138+
*/
139+
PHP_FUNCTION(c_fib)
140+
{
141+
zend_long number = 0;
142+
zend_long result = 0;
143+
ZEND_PARSE_PARAMETERS_START(0, 1)
144+
Z_PARAM_OPTIONAL
145+
Z_PARAM_LONG(number)
146+
ZEND_PARSE_PARAMETERS_END();
147+
148+
if (number == 0) {
149+
RETURN_LONG(result);
150+
} else {
151+
result = fibinC(number);
152+
RETURN_LONG(result);
153+
}
154+
}
155+
/* }}}*/
156+
ZEND_BEGIN_ARG_INFO(arginfo_c_fib, 0)
157+
ZEND_ARG_INFO(0, number)
158+
ZEND_END_ARG_INFO()
159+
```
160+
127161
## 重新编译运行
128162

129163
这里编译需要增加对我们之前动态链接库的引用。
@@ -153,10 +187,11 @@ echo '[Rust]Ext执行时间:' . (microtime(true) - $time_start).PHP_EOL;
153187
执行`make test-php`,运行结果如下:
154188

155189
```
156-
[PHP]fib执行时间:36.620906829834
157-
[Rust]Debug执行时间:21.764172077179
158-
[Rust]Release执行时间:6.7895750999451
159-
[Rust]Ext执行时间:4.9096128940582
190+
[PHP]fib执行时间:35.900850057602
191+
[Rust]Debug执行时间:22.036439180374
192+
[Rust]Release执行时间:6.8401432037354
193+
[Rust]Ext执行时间:5.19260597229
194+
[C]Ext执行时间:13.217513799667
160195
```
161196
看来用Rust开发PHP的扩展性能上面是完全可以的。
162197

ext/rust/rust.c

+33-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
#include "ext/standard/info.h"
99
#include "php_rust.h"
1010

11+
// 这里直接编写一个C的fib调用
12+
int fibinC(int n)
13+
{
14+
if (n <= 1)
15+
return n;
16+
return fibinC(n - 1) + fibinC(n - 2);
17+
}
18+
1119

1220
/* For compatibility with older PHP versions */
1321
#ifndef ZEND_PARSE_PARAMETERS_NONE
@@ -45,7 +53,6 @@ PHP_FUNCTION(rust_test2)
4553
}
4654
/* }}}*/
4755

48-
// rust fib函数导入到PHP中的具体实现
4956
/* {{{ int rust_fib( [ int $var ] )
5057
*/
5158
PHP_FUNCTION(rust_fib)
@@ -66,6 +73,26 @@ PHP_FUNCTION(rust_fib)
6673
}
6774
/* }}}*/
6875

76+
/* {{{ int c_fib( [ int $var ] )
77+
*/
78+
PHP_FUNCTION(c_fib)
79+
{
80+
zend_long number = 0;
81+
zend_long result = 0;
82+
ZEND_PARSE_PARAMETERS_START(0, 1)
83+
Z_PARAM_OPTIONAL
84+
Z_PARAM_LONG(number)
85+
ZEND_PARSE_PARAMETERS_END();
86+
87+
if (number == 0) {
88+
RETURN_LONG(result);
89+
} else {
90+
result = fibinC(number);
91+
RETURN_LONG(result);
92+
}
93+
}
94+
/* }}}*/
95+
6996
/* {{{ PHP_RINIT_FUNCTION
7097
*/
7198
PHP_RINIT_FUNCTION(rust)
@@ -97,10 +124,13 @@ ZEND_BEGIN_ARG_INFO(arginfo_rust_test2, 0)
97124
ZEND_ARG_INFO(0, str)
98125
ZEND_END_ARG_INFO()
99126

100-
// 这里处理参数
101127
ZEND_BEGIN_ARG_INFO(arginfo_rust_fib, 0)
102128
ZEND_ARG_INFO(0, number)
103129
ZEND_END_ARG_INFO()
130+
131+
ZEND_BEGIN_ARG_INFO(arginfo_c_fib, 0)
132+
ZEND_ARG_INFO(0, number)
133+
ZEND_END_ARG_INFO()
104134
/* }}} */
105135

106136
/* {{{ rust_functions[]
@@ -109,6 +139,7 @@ static const zend_function_entry rust_functions[] = {
109139
PHP_FE(rust_test1, arginfo_rust_test1)
110140
PHP_FE(rust_test2, arginfo_rust_test2)
111141
PHP_FE(rust_fib, arginfo_rust_fib)
142+
PHP_FE(c_fib, arginfo_c_fib)
112143
PHP_FE_END
113144
};
114145
/* }}} */

test-ffi.php

+9
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,13 @@ function fib($n) {
5555

5656
echo '[Rust]Ext执行时间:' . (microtime(true) - $time_start).PHP_EOL;
5757

58+
59+
// 这里我们采用C编写的一个PHP扩展
60+
$time_start = microtime(true);
61+
for ($i=0; $i < 10000000; $i++) {
62+
$v = c_fib(12);
63+
}
64+
65+
echo '[C]Ext执行时间:' . (microtime(true) - $time_start).PHP_EOL;
66+
5867
?>

0 commit comments

Comments
 (0)