Thread in_array() - Funktion bauen (25 answers)
Opened by Gast at 2005-10-13 15:58

pq
 2005-10-13 23:28
#58783 #58783
User since
2003-08-04
12209 Artikel
Admin1
[Homepage]
user image
ich habs eben auch ausprobiert, das ist aber sicher nicht im sinne des moduls.
sorry, da habe ich mich dann wohl vertan.
mich wundert es aber ehrlich gesagt, auch wenn ich statt 45000 nur 5000
als vergleich nehme. ist grep evtl. auf skalaren kontext optimiert?

edit: ich hab was entdeckt
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
my $f = 50;
use Benchmark;
use strict;
use List::Util qw(first);
my @x = (0..$ARGV[0]||1000);
Benchmark::timethese(-2, {
Grep => sub{my $y  = grep {$_ == $f} @x},
Util => sub {my $y = first {$_ == $f} @x} });
' 100
Benchmark:
running
Grep, Util
for at least 2 CPU seconds
...

     Grep:  2 wallclock secs ( 2.00 usr +  0.00 sys =  2.00 CPU) @ 46457.50/s (n=92915)

     Util:  2 wallclock secs ( 2.00 usr +  0.14 sys =  2.14 CPU) @ 76733.18/s (n=164209)

also wenn ich eine subref übergebe statt eines stirngs, ist Util schneller.
wenn ich @x noch größer mache, wird es entsprechend noch schneller...\n\n

<!--EDIT|pq|1129232849-->
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem

View full thread in_array() - Funktion bauen