Thread string nach jeder 4000 Stelle ein TAB einsetzten (29 answers)
Opened by mr-sansibar at 2007-08-02 13:42

bloonix
 2007-08-02 16:37
#97322 #97322
User since
2005-12-17
1615 Artikel
HausmeisterIn
[Homepage]
user image
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use strict;
use warnings;
use Benchmark;

my $tmp_line = 'x' x 18_000;

open my $frontend, '>>', '/dev/null' or die $!; 

Benchmark::cmpthese(-1, {
   _regex   => \&_regex,
   _unpack1 => \&_unpack1,
   _unpack2 => \&_unpack2
});

sub _regex {
    if( length $tmp_line < 4000 ){
        $tmp_line .= "\t" x 5;
    }   
    else{
        $tmp_line =~ s/(.{4000})/$1\t/g;
    }   
    print $frontend $tmp_line;
}

sub _unpack1 {
    my $laenge = length($tmp_line); 
    if($laenge <=4000) {
        print $frontend "$tmp_line\t\t\t\t\n";
    }   
    elsif($laenge <= 8000) {
        my ($a, $b) = unpack("A4000 A4000", $tmp_line);
        print $frontend "$a\t$b\t\t\t\n";
    }   
    elsif($laenge <= 12000) {
        my ($a, $b, $c) = unpack("A4000 A4000 A4000", $tmp_line);
        print $frontend "$a\t$b\t$c\t\t\n";
    }   
    elsif($laenge <= 16000) {
        my ($a, $b, $c, $d) = unpack("A4000 A4000 A4000 A4000", $tmp_line);
        print $frontend "$a\t$b\t$c\t$d\t\n";
    }   
}

sub _unpack2 {
    my $len = length($tmp_line);
    my $pos = 0;
    my $tab = 5;
    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";
}


Ausgabe:

Code: (dl )
1
2
3
4
              Rate   _regex _unpack2 _unpack1
_regex 1172/s -- -98% -100%
_unpack2 48188/s 4012% -- -97%
_unpack1 1557667/s 132813% 3133% --
What is a good module? That's hard to say.
What is good code? That's also hard to say.
One man's Thing of Beauty is another's man's Evil Hack.

View full thread string nach jeder 4000 Stelle ein TAB einsetzten