gpt4 book ai didi

c - 正则表达式与管道在速度方面的比较

转载 作者:行者123 更新时间:2023-11-30 18:02:18 25 4
gpt4 key购买 nike

我正在为 nmap 编写一个简单的 ncurses GUI 包装器在 Linux 下,使阅读和理解输出变得更简单。但是,在解析输出时,使用 POSIX 正则表达式并计算代码中的每个表达式更快,或者将 nmap 输出通过管道传输到诸如 grep、sed 等实用程序或剪切

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

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供测试用。如果您需要基准方面的帮助,请告诉我。

关于c - 正则表达式与管道在速度方面的比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9252213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com