【发布时间】:2020-02-23 02:50:48
【问题描述】:
在下面的示例中,PHP 比 Rust 快 5.5 倍。
我做错了什么?
在我看来,Rust 中的正则表达式引擎似乎比 PHP 中的要慢。
PHP 代码:
$html = file_get_contents('/path_to/test.html');
global $c_id;
$c_id = 0;
echo 'len with comments: ', strlen($html), "\n";
$time_start = microtime(true);
$html = preg_replace_callback('/<!--(.*?)-->/s', function($matches) {
global $c_id;
$c_id++;
return str_replace($matches[1], $c_id, $matches[0]);
}, $html);
echo (microtime(true) - $time_start), " seconds for removing comments.\n";
echo 'len without comments: ', strlen($html), "\n";
锈代码:
use regex::Regex;
use std::io::prelude::*;
use std::fs::File;
fn main() {
let mut file = File::open("/path_to/test.html").expect("Unable to open the file");
let mut html = String::new();
file.read_to_string(&mut html).expect("Unable to read the file");
let mut c_id: usize = 0;
println!("len with comments: {}", html.len());
let start = PreciseTime::now();
let re = Regex::new(r"(?s)<!--(.*?)-->").unwrap();
html = re.replace_all(&html, |captures: ®ex::Captures| {
c_id += 1;
captures[0].replace(&captures[1], &c_id.to_string())
}).to_string();
println!("{} seconds for removing comments.", start.to(PreciseTime::now()));
println!("len without comments: {}", html.len());
}
Rust 依赖项:
regex = "1"
time = "*"
结果
PHP:
len with comments: 76968
0.00019717216491699 seconds for removing comments.
len without comments: 76622
生锈:
len with comments: 76968
PT0.001093365S seconds for removing comments.
len without comments: 76622
谢谢!
【问题讨论】:
-
您是否使用
--release构建/运行了 Rust 程序? -
preg_replace_callback应该返回替换字符串。你为什么不直接返回"<!--$c_id-->"而不是调用str_replace,这可能会得到相同的结果,但代价更高? -
是的,但这是一个特定的例子。如果您的正则表达式不同,那将无法正常工作。
-
是的,它是用release模式编译的。
-
您没有提供可重现的基准,因此实际上不可能回答这个问题。请提供所有输入,包括您的语料库。
标签: php regex performance optimization rust