繁体   English   中英

正则表达式与管道的速度

[英]Regex vs. Piping in terms of speed

我正在为Linux下的nmap编写一个简单的ncurses GUI包装程序,以使其更易于阅读和理解输出。 但是,在解析输出时,使用POSIX正则表达式并评估我的代码中的每个表达式,还是将nmap输出通过管道传递给grepsedcut等实用工具,速度更快?

例如,如果我想在子网中检索在线主机,那么以下哪种解决方案可能更好?

pipe = popen("nmap -sn xxx.xxx.xxx.xxx/24", "r");
if (pipe == NULL) {
    fprintf (stderr, "error creating the pipe: %s\n", strerror(errno));
    exit (1);
}

while (!feof (pipe)) {
    if (fgets (buff, BUFF_SIZE, pipe) != NULL)  {

        /* perform regex evaluations here */

        printf ("%s", buff);
    }
}

pclose (pipe);

pipe = popen("nmap -sn xxx.xxx.xxx.xxx/24 | grep -E 'pattern' | ...", "r");
if (pipe == NULL) {
    fprintf (stderr, "error creating the pipe: %s\n", strerror(errno));
    exit (1);
}

while (!feof (pipe)) {
    if (fgets (buff, BUFF_SIZE, pipe) != NULL)  {
        printf ("%s", buff);
    }
}

pclose (pipe);

做你的基准。 我建议使用http://goo.gl/E3jbM进行测试。 如果您需要基准测试方面的帮助,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM