in reply to How to process each byte in a binary file?
Looks like unpack() is the clear winner on my machine:
Gives me:#!/usr/bin/perl use Benchmark; my $string="X" x 102400; timethese(100, { 'split' => sub { for (split(//,$string)) {}; }, 'unpack' => sub { for (unpack("C*",$string)) {}; }, 'regex' => sub { while($string =~ /./sg) {} }, 'substr' => sub { for(my $i=0;$i<length($string);$i++){ substr($string,$i,1); } }, });
$ perl foo
Benchmark: timing 100 iterations of regex, split, substr, unpack...
regex: 44 wallclock secs (43.13 usr + 0.00 sys = 43.13 CPU)
split: 49 wallclock secs (47.90 usr + 0.04 sys = 47.94 CPU)
substr: 58 wallclock secs (55.70 usr + 0.00 sys = 55.70 CPU)
unpack: 27 wallclock secs (26.48 usr + 0.00 sys = 26.48 CPU)
Update:Reposted results after correcting typo.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: How to process each byte in a binary file?
by John M. Dlugosz (Monsignor) on Aug 13, 2002 at 00:46 UTC | |
by kschwab (Vicar) on Aug 13, 2002 at 03:05 UTC |