in reply to Inefficient code?

I don't know if it's faster than opening a pipe from gunzip like you do, but any time I need to read .gz files I use Compress::Zlib. Since it's an actual module, it may be faster than opening the pipe (especially if called repeatedly) since that probably involves spawning a new process. Compress::Zlib is easy to use and lets you read directly from the gzipped files. Here's a simple gzcat example taken directly from the Compress::Zlib docs:

use strict ; use warnings ; use Compress::Zlib ; die "Usage: gzcat file...\n" unless @ARGV ; my $file ; foreach $file (@ARGV) { my $buffer ; my $gz = gzopen($file, "rb") or die "Cannot open $file: $gzerrno\n" ; print $buffer while $gz->gzread($buffer) > 0 ; die "Error reading from $file: $gzerrno" . ($gzerrno+0) . "\n" + if $gzerrno != Z_STREAM_END ; $gz->gzclose() ; }

Replies are listed 'Best First'.
Re: Re: Inefficient code?
by Anonymous Monk on Apr 23, 2001 at 10:42 UTC
    Thanks all for your suggestions. By using Benchmark and Compress:Zlib on 10 files (each about 47K each), I found the following:
    pre compress::zlib: the code took:17 wallclock secs (11.76 usr 2.52 sys + 0.25 cusr 0.2 +5 csys = 14.78 CPU) with compress:zlib: the code took:15 wallclock secs ( 9.71 usr 2.60 sys + 0.02 cusr 0.0 +6 csys = 12.39 CPU) Not much of a difference, but for more files, I found the saving in time up to 10 seconds. Thanks once again! Regards, Stacy.