Thread Performanz der bedingung der for-Schleife (4 answers)
Opened by pktm at 2009-05-09 13:46

Linuxer
 2009-05-09 13:56
#121378 #121378
User since
2006-01-27
3891 Artikel
HausmeisterIn

user image
Testcode:
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
#!/usr/bin/perl -l
use strict;
use warnings;

sub slowly {
sleep 1;
return 4;
}

print "1. C-like with direct call";
for ( my $x=1; $x < slowly(); $x++ ) {
print $x;
}

print "2. C-like with variable";
my $max = slowly();
for ( my $x=1; $x < $max; $x++ ) {
print $x;
}

print "3. perlish for";
for my $x ( 1 .. slowly()-1 ) {
print $x;
}


Anhand der Ausgabe ist zu sehen, dass slowly() für jeden Durchlauf aufgerufen wird.
Es kann also wirklich performanter sein, das Ergebnis von slowly() zunächst in eine Variable zu packen.

Oder man verwendet gleich die dritte Variante.
meine Beiträge: I.d.R. alle Angaben ohne Gewähr und auf Linux abgestimmt!
Die Sprache heisst Perl, nicht PERL. - Bitte Crossposts als solche kenntlich machen!

View full thread Performanz der bedingung der for-Schleife