首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从速度角度看Regex和Piping

从速度角度看Regex和Piping
EN

Stack Overflow用户
提问于 2012-02-12 20:03:07
回答 1查看 122关注 0票数 0

我正在为Linux下的nmap编写一个简单的ncurses包装器,以便更容易地阅读和理解输出。但是,在解析输出时,使用POSIX正则表达式并计算代码中的每个表达式,或者将nmap输出输送到实用程序(如grepsedcut )是否更快?

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

代码语言:javascript
复制
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);

代码语言:javascript
复制
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);
EN

回答 1

Stack Overflow用户

发布于 2012-02-13 01:08:52

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9252213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档