Benchmarking cat versus open() in perl

Benchmarking cat versus open() in perl


Thanks to my old friend billtcat back in '97, I've picked up a naughty perl trick I can't seem to shake.
  • @array = `cat filename`;   #opening filehandles is too much work!
    
  • But that is a system call!! GASP that must REALLY slow things down!
    Ah, but does it? I decided to find out, here are the benchmark results of the program i wrote to test this
  • Reading 1MB_of_zeros.dat
               Rate  Cat File Open_File
    Cat File  142/s        --       -9%
    Open_File 156/s       10%        --
    
  • So, in order to actually benchmark this i had to run the commands a few times each (for example, cat_file was run 142 times a second for 30seconds!)
    In the time it took me to cat a file 4260 times i could have opened 4680 files!
    I think i'm going to keep using cat to read in single files :D


    Here are some more runs with 10MB and 100MB files. I tried to test 1000MB but i ran out of memory :D
  • Reading 10MB_of_zeros.dat
                Rate  Cat File Open_File
    Cat File  13.0/s        --      -17%
    Open_File 15.6/s       20%        --
    
  • Reading 100MB_of_zeros.dat
                Rate  Cat File Open_File
    Cat File  1.28/s        --      -19%
    Open_File 1.59/s       24%        --
    


  • System specs: Perl 5.8.8 Single proc, fedora 8