【问题标题】:Rust regex replace_all slower than PHP regex preg_replace_callback, how to optimize?Rust regex replace_all 比 PHP regex preg_replace_callback 慢,如何优化?
【发布时间】: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: &regex::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 应该返回替换字符串。你为什么不直接返回"&lt;!--$c_id--&gt;" 而不是调用str_replace,这可能会得到相同的结果,但代价更高?
  • 是的,但这是一个特定的例子。如果您的正则表达式不同,那将无法正常工作。
  • 是的,它是用release模式编译的。
  • 您没有提供可重现的基准,因此实际上不可能回答这个问题。请提供所有输入,包括您的语料库。

标签: php regex performance optimization rust


【解决方案1】:

答案是在rust中使用pcre2 crate代替regex crate。

更多信息可以在这里找到:https://users.rust-lang.org/t/rust-regex-replace-all-slower-than-php-regex-preg-replace-callback-how-to-optimize/34036/20

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-15
    • 2018-08-10
    • 1970-01-01
    • 2012-05-12
    • 2011-08-25
    • 1970-01-01
    • 2012-06-13
    • 2016-03-06
    相关资源
    最近更新 更多