Thread Anzahl Zeilen in Textdatei ermitteln: (Wie mache ich das am schnellsten?) (34 answers)
Opened by Crian at 2004-04-21 11:58

Crian
 2004-04-21 17:49
#81724 #81724
User since
2003-08-04
5866 Artikel
ModeratorIn
[Homepage]
user image
stimmt :-D


Ich hab auch nochmal gebenchmarked mit komischem Ergebnis:

Code: (dl )
1
2
3
4
5
6
7
C:\Daten\perl>benchmark.pl
Benchmark: timing 100 iterations of A, A2, C, D...
A: 307 wallclock secs (272.94 usr + 27.55 sys = 300.49 CPU) @ 0.33/s (n=100)
A2: 316 wallclock secs (278.64 usr + 24.92 sys = 303.56 CPU) @ 0.33/s (n=100)
C: 333 wallclock secs (278.80 usr + 23.83 sys = 302.63 CPU) @ 0.33/s (n=100)
D: 0 wallclock secs ( 0.00 usr + 0.00 sys = 0.00 CPU)
(warning: too few iterations for a reliable count)


Der Code dazu:

Code: (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
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark;

my $file = 'test.txt';

timethese(100, {
A => sub {
open ( FILE, $file );
my $ln;
$ln++ while <FILE>;
close (FILE);
},

A2 => sub {
my $ln;
open ( FILE, $file );
++$ln while <FILE>;
close (FILE);
},
# ist unter C ein Unterschied, spart temporäre Variable...

#B => sub {
# open ( FILE, $file );
# my @array;
# my $ln = scalar(@array = <FILE>);
# close (FILE);
# },

C => sub {
open ( FILE, $file );
while ( <FILE> ) {}
my $ln = $.;
close (FILE);
},

D => sub {
my $ln = -s $file;
},
}
);

# test.txt hat 3.804.144 Zeilen und ist 195.338.421 Byte groß


Bei B hat er so wild angefangen zu swappen, dass ich die Variante rausgeworfen habe.

-s geht rasant schnell... hmmm da muss ich doch wohl in den saueren Apfel beißen und die Zeichen jeder Zeile ermitteln...
s--Pevna-;s.([a-z]).chr((ord($1)-84)%26+97).gee; s^([A-Z])^chr((ord($1)-52)%26+65)^gee;print;

use strict; use warnings; Link zu meiner Perlseite

View full thread Anzahl Zeilen in Textdatei ermitteln: (Wie mache ich das am schnellsten?)