Schrift
[thread]6700[/thread]

Konstanten in Modulen (Seite 2)



<< |< 1 2 >| >> 14 Einträge, 2 Seiten
format_c
 2005-02-16 00:59
#51667 #51667
User since
2003-08-04
1706 Artikel
HausmeisterIn
[Homepage] [default_avatar]
Mich hat das jetzt mal interessiert und es kam folgendes dabei raus:
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
koeppea@foxi:~/perl_scripts> cat benchmark_constant.pl
use strict;
use Benchmark;

my $startzeit = time;

local $| = 1;
sub test1 {
system(qq{perl -e 'use constant NAME => "Alex";print STDERR NAME;' 2> /dev/null});
}

sub test2 {
system(qq{perl -e 'sub NAME {"Alex"};print STDERR NAME;' 2> /dev/null});
}

sub test3 {
use constant NAME1 => "Alex";
print STDERR NAME1;
}

sub test4 {
sub NAME2 {"Alex"};
print STDERR NAME2;
}

timethese(1_000_000,{
"\nEinzeln Kompiliert und interpretiert (use constant): " => \&test1,
"\nEinzeln Kompiliert und interpretiert (sub {} ): " => \&test2,
"\nEinmal Kompiliert und einzeln interpretiert (use constant): " => \&test3,
"\nEinmal Kompiliert und einzeln interpretiert (sub {} ): " => \&test4
});

print "\n===================================================================\n";
my $endzeit = time;
printf ("Benchmark begann: %02d:%02d:%02d\n",reverse ((localtime($startzeit))[0..2]));
printf ("Benchmark beendet: %02d:%02d:%02d\n",reverse ((localtime($endzeit))[0..2]));
printf ("Benchmarkdauer: %02d:%02d:%02d\n",reverse ((localtime($endzeit - $startzeit))[0..2]));
koeppea@foxi:~/perl_scripts> perl benchmark_constant.pl 2> /dev/null 1> benchmark_results.txt

koeppea@foxi:~/perl_scripts> cat benchmark_results.txt
Benchmark: timing 1000000 iterations of
Einmal Kompiliert und einzeln interpretiert (sub {} ): ,
Einmal Kompiliert und einzeln interpretiert (use constant): ,
Einzeln Kompiliert und interpretiert (sub {} ): ,
Einzeln Kompiliert und interpretiert (use constant): ...

Einmal Kompiliert und einzeln interpretiert (sub {} ): : 2 wallclock secs ( 2.22 usr + 0.34 sys = 2.56 CPU) @ 390625.00/s (n=1000000)

Einmal Kompiliert und einzeln interpretiert (use constant): : 1 wallclock secs ( 0.76 usr + 0.23 sys = 0.99 CPU) @ 1010101.01/s (n=1000000)

Einzeln Kompiliert und interpretiert (sub {} ): : 13282 wallclock secs (79.45 usr 687.58 sys + 6983.66 cusr 5386.81 csys = 13137.50 CPU) @ 1303.73/s (n=1000000)

Einzeln Kompiliert und interpretiert (use constant): : 30559 wallclock secs (66.26 usr 643.40 sys + 22127.14 cusr 7380.62 csys = 30217.42 CPU) @ 1409.13/s (n=1000000)

===================================================================
Benchmark begann: 09:51:01
Benchmark beendet: 22:01:53
Benchmarkdauer: 13:10:52
koeppea@foxi:~/perl_scripts>


Gruß Alex
format_c
 2005-02-16 01:10
#51668 #51668
User since
2003-08-04
1706 Artikel
HausmeisterIn
[Homepage] [default_avatar]
Bei der einzelnen Kompilierung ist der vorteil von use constant nicht mehr so deutlich jedoch immer noch schneller als die variante mit sub oder interpretiere ich das ergebnis falsch?

Gruß Alex
esskar
 2005-02-16 01:34
#51669 #51669
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
???

use constant NAME bzw. sub NAME wird beim kompilieren genau 1 mal ausgeführt! :)
Der rest der zeit kommt wieder zusammen, da du wieder alles auf STDERR ausgegeben wird!!!
Gast Gast
 2005-02-20 21:44
#51670 #51670
[quote=format_c,13.02.2005, 14:43]
Code: (dl )
1
2
3
4
5
6
7
8
package mymodule;
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(NAME);

use constant NAME => 'Alex';
package main;
print NAME;
[/quote]
Versuch ..
Code: (dl )
1
2
3
4
5
6
7
8
package mymodule;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(NAME);

use constant NAME => 'Alex';
package main;
print NAME;

oder, falls es in andere Dateien eingebunden werden soll:
Code: (dl )
1
2
3
4
5
6
package MyModule; # module sollten mit grossbuchstaben sein
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(NAME);

use constant NAME => 'Alex';

Code: (dl )
1
2
use MyModule qw(NAME);
print NAME;
<< |< 1 2 >| >> 14 Einträge, 2 Seiten



View all threads created 2005-02-13 15:43.