Thread Performanceangaben unpack vs. regex (10 answers)
Opened by mr-sansibar at 2007-09-24 14:17

mr-sansibar
 2007-09-24 15:01
#99918 #99918
User since
2006-04-13
90 Artikel
BenutzerIn
[default_avatar]
esskar+2007-09-24 12:53:21--
wie hast du denn getestet?
mich wundert, dass regexp überhaupt schneller ist als unpack.
kannst du mal deine snippets posten?



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";
}

View full thread Performanceangaben unpack vs. regex