Re: Perls Listen

David Haller <[email protected]>
Newsgroups gmane.linux.suse.programming
Message-ID <[email protected]>
Hallo,

Am Tue, 28 Jun 2005, Ferdinand Ihringer schrieb:
>Weiß hier jemand zufÀlligerweise, ob man in Perl durch irgendein Modul die 
>Listen durch echte Arrays mit ein konstanten Zugriffszeit auf ein beliebiges 
>Element ersetzen kann? Das ist fÃŒr meinen Fall hier jetzt nicht wichtig, aber 
>O(n) wÀre schon ein bisschen schöner als O(n^2).

$ cat perlarray.pl
#!/usr/bin/perl -w
use Benchmark qw(timethese);
open(DN, ">/dev/null") or die "$!\n";
foreach my $l (@ARGV) {
  print "using array of $l elements\n";
  timethese(100, { "loop" => sub {
                     my @arr;
                     for($i=0; $i < $l; ++$i) {
                       $arr[$i]=$i;
                     };
                     for($i=0; $i < $#arr; ++$i) {
                       print DN $arr[$i/2];
                     };
                   }
                 });
}
close(DN);
$ perl perlarray.pl 1000 10000 100000
using array of 1000 elements
Benchmark: timing 100 iterations of loop...
      loop:  1 wallclock secs ( 1.19 usr +  0.00 sys =  1.19 CPU) @ 84.03/s (n=100)
using array of 10000 elements
Benchmark: timing 100 iterations of loop...
      loop: 13 wallclock secs (12.20 usr +  0.00 sys = 12.20 CPU) @  8.20/s (n=100)
using array of 100000 elements
Benchmark: timing 100 iterations of loop...
      loop: 127 wallclock secs (126.56 usr +  0.03 sys = 126.59 CPU) @  0.79/s (n=100)

Fuer mich sieht das verdammt nach O(n) aus.

-dnh

-- 
Q: What's the difference between a Used Car SalesPerson and a
   Computer SalesPerson?
A: The Used Car SalesPerson KNOWS when they're lying.

-- 
Um die Liste abzubestellen, schicken Sie eine Mail an:
    [email protected]
Um eine Liste aller verfÃŒgbaren Kommandos zu bekommen, schicken
Sie eine Mail an: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.