简体   繁体   中英

Regex vs. Piping in terms of speed

I'm writing a simple ncurses GUI wrapper for nmap under Linux, to make it simpler to read and understand the ouput. However, when it comes to parsing the output is it faster to use POSIX regex and evaluate every expression in my code, or pipe the nmap output to utilities such as grep , sed or cut ?

For example, if I want to retrieve online hosts in a subnet, which of the following solutions could be better?

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);

vs.

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);

Do your benchmark. I recomend to use http://goo.gl/E3jbM for testing. Please let me know if you need help on the benchmark.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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