Thread Performanceangaben unpack vs. regex
(10 answers)
Opened by mr-sansibar at 2007-09-24 14:17 esskar+2007-09-24 12:53:21-- Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 use strict; use warnings; use Benchmark; my $line = 'x' x 250; open my $frontend, '>>', '/dev/null' or die $!; Benchmark::cmpthese(-1, { _regex => \&_regex, _unpack2 => \&_unpack2 }); sub _regex { my $tmp_line = $line; if( length $tmp_line < 4000 ){ $tmp_line .= "\t" x 30; } else{ $tmp_line =~ s/(.{4000})/$1\t/g; } print $frontend $tmp_line; } sub _unpack2 { my $tmp_line = $line; my $len = length($tmp_line); my $pos = 0; my $tab = 30; while ( my $s = unpack("x$pos A4000", $tmp_line) ) { $tab--; print $frontend "$s\t"; $pos += 4000; last if $pos >= $len; } print $frontend "\t" x $tab; print $frontend "\n"; } |