On Fri, 8 Jun 2001, Richard Hulse wrote:
> Since someone raised the general question of differences, which is faster?
> my @result = <*.jpg>;
> or variations on:
> @files = grep /jpg/i, readdir DIR;

use Benchmark;
timethese(10000, {
'Glob' => sub { my @result = <*.jpg>; },
'Read' => sub { opendir(DIR,"."); @files = grep /jpg/i, readdir DIR; }, });

Benchmark: timing 10000 iterations of Glob, Read...
     Glob: 78 wallclock secs (49.36 usr + 27.43 sys = 76.79 CPU)
           @ 130.23/s (n=10000)
     Read: 32 wallclock secs (27.71 usr +  3.69 sys = 31.40 CPU)
           @ 318.47/s (n=10000)

Looks like readdir() is the winner, at least on my system.
This was done in a directory of 1000 jpg images.

The times certianly look right... the glob uses a ton of system time, and
the readdir uses a ton of user time, just as one would expect.

--
Ian

Reply via email to