Thread Schleifengeschwindigkeit
(20 answers)
Opened by RPerl at 2009-05-30 17:02
Ich habe mal die Objektorientierte Variante umgangen:
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 #!/usr/bin/perl use strict; use warnings; use Net::IP qw(&ip_inttobin &ip_bintoip); use Benchmark qw/:all/; sub new { my $ip = new Net::IP('217.229.0.134 - 217.229.0.150') or die; my @ips; my $intip=$ip->intip(); while($intip < $ip->last_int()) { $intip++; push(@ips, ip_bintoip(ip_inttobin($intip, $ip->version), $ip->version)); } } sub old { my $ip = new Net::IP('217.229.0.134 - 217.229.0.150') or die; my @ips = (); while ($ip) { push @ips, $ip->ip(); ++$ip; } } my $result = timethese( 500, { 'new' => \&new, 'old' => \&old } ); und was soll ich sagen: Code: (dl
)
1 Benchmark: timing 500 iterations of new, old... Es ist schneller. Der Grund ist, dass bei jedem "++$ip" ein neues IP-Objekt erzeugt und das alte verworfen wird. Das kostet enorm Zeit. |