Rust 写PHP的C扩展,性能超级提升。
最近在学习rust,顺便在github上找些开源的项目学习下,正好看到用rust开发php扩展的库,经过本人实践和学习之后,通过一个简单的斐波那契计算函数,来体验一下rust在开发php扩展方面的优势。
使用的rust库 ext-php-rs
github地址 github.com/davidcole1340/ext-php-r...
代码:
#![cfg_attr(windows, feature(abi_vectorcall))]
use ext_php_rs::prelude::*;
#[php_function]
pub fn hello_world(name: &str) -> String {
format!("Hello, {}!", name)
}
#[php_function]
pub fn get_sum(n: usize) -> String {
let mut sum =0;
for _i in 0..n {
sum +=n*10;
}
sum.to_string()
}
#[php_function]
pub fn calc_fib(i:u32)->u32 {
if i <2 {
return i
}
return calc_fib(i-1)+calc_fib(i-2)
}
#[php_module]
pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
module
}
截图
运行时间
rust: 0.0027499198913574 秒
之前我有用go的cgo开发的扩展和php原生写法进行过相同的测试,感觉rust在性能和书写难易程度上,还是很有优势的。
本作品采用《CC 协议》,转载必须注明作者和本文链接
大佬牛哇。Rust这门语言还是很少听说的,经常应用于什么领域的
怎么打包成php拓展