system() vs. internal code

Jim Hart <[email protected]> Sun, 17 Apr 2005 22:57:33 -0400
Newsgroups gmane.comp.web.wiki.awkiawki.user
Message-ID <[email protected]>
Some of my co-workers, in comparing languages, have argued that 
spawning processes, in a Web environment, is expensive. Thus, they 
argue for monolithic languages, such as PHP and Perl, rather than using 
multiple Unix utilities to do the same job.

In the vein of this argument, I've wondered about spawning processes in 
AWK vs. doing the job in AWK code. One such case is testing for the 
existence of a file. So, I thought I'd do some simple timing tests. The 
amount of time, regardless of method, is pretty small, so it probably 
isn't significant in any practical sense. Nonetheless, the times for 
reading a record from the file and closing the file are remarkably 
consistent at .011 - .012 seconds. But the time to spawn a "test" 
command can be nearly 5 times as long with AWK at .058 seconds. These 
tests were run on a Powerbook G4 667 Mhz. Each step was run twice. The 
first time gets the AWK/GAWK/MAWK program cached. Otherwise, the time 
to load the program overshadows the time to run the code. It's also 
interesting to note that mawk is much faster at spawning a system() 
task than AWK and GAWK, suggesting that much of the overhead is in the 
spawning program, rather than in the operating system.

Does this mean we should recode the "test -f ..." in awkiawki? Well, 
again, the amount of time is pretty small. On the other hand, using 
"getline" will not only produce an error if the file doesn't exist, it 
will also fail if the file isn't readable. That's potentially useful 
information. OTOH, one could use "test -r ..." to make the same 
determination.

I apologize, in advance, if this kind of stuff bores you to tears.

Jim Hart


The results.
=========

With GAWK:

localhost ~/Src/awkiawki bash2.05$ time gawk 'BEGIN{system("test -f 
awki.cgi")}'

real    0m0.038s
user    0m0.020s
sys     0m0.010s
localhost ~/Src/awkiawki bash2.05$ time gawk 'BEGIN{getline < 
"awki.cgi";close("awki.cgi")}'

real    0m0.012s
user    0m0.010s
sys     0m0.000s

With AWK:

localhost ~/Src/awkiawki bash2.05$ time awk 'BEGIN{system("test -f 
awki.cgi")}'

real    0m0.058s
user    0m0.010s
sys     0m0.010s
localhost ~/Src/awkiawki bash2.05$ time awk 'BEGIN{getline < 
"awki.cgi";close("awki.cgi")}'

real    0m0.012s
user    0m0.010s
sys     0m0.000s

With mawk:

localhost ~/Src/mawk-1.3.3 bash2.05$ time mawk 'BEGIN{system("test -f 
awki.cgi")}'

real    0m0.021s
user    0m0.020s
sys     0m0.000s
localhost ~/Src/mawk-1.3.3 bash2.05$ time mawk 'BEGIN{getline < 
"awki.cgi";close("awki.cgi")}'

real    0m0.011s
user    0m0.000s
sys     0m0.010s



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click