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. In reply to Re: How to process each byte in a binary file?
by kschwab
in thread How to process each byte in a binary file?
by John M. Dlugosz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |