簡體   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