in reply to Re^2: given-when construct unexpected bahavior wit arrays
in thread given-when construct unexpected behavior with arrays
Ok, this is the last testcase:
#!/usr/bin/env perl # ex: set tabstop=4 noexpandtab: use v5.14; use warnings; use Benchmark qw/timethis/; my $count = shift || 10_000; sub grep_in_array { my ($element, @array) = @_; grep {$element eq $_} @array and return 1; return 0; } sub is_in_array { my ($element, @array) = @_; given ("_$element") { when ([map {"_$_"} @array]) { return 1; } } return 0; } my @array = (0..10_000, 'abcd'); for my $element (qw/a ab abc 0 1 10 100 1000 10000 10001 abcd/) { say "Test with: $element"; say "With grep:"; say sprintf("Element %s %s in array", $element, grep_in_array($elem +ent, @array) ? "is" : "is not"); say "With given-when:"; say sprintf("Element %s %s in array", $element, is_in_array($elem +ent, @array) ? "is" : "is not"); say "With grep:"; timethis($count, sub { grep_in_array($element, @array); }); say "With given-when:"; timethis($count, sub { is_in_array($element, @array); }); }
And the winner is: grep
Test with: a With grep: Element a is not in array With given-when: Element a is not in array With grep: timethis 1000: 2 wallclock secs ( 2.17 usr + 0.00 sys = 2.17 CPU) @ + 460.83/s (n=1000) With given-when: timethis 1000: 8 wallclock secs ( 7.34 usr + 0.00 sys = 7.34 CPU) @ + 136.24/s (n=1000) Test with: ab With grep: Element ab is not in array With given-when: Element ab is not in array With grep: timethis 1000: 2 wallclock secs ( 2.20 usr + 0.00 sys = 2.20 CPU) @ + 454.55/s (n=1000) With given-when: timethis 1000: 8 wallclock secs ( 7.66 usr + 0.00 sys = 7.66 CPU) @ + 130.55/s (n=1000) Test with: abc With grep: Element abc is not in array With given-when: Element abc is not in array With grep: timethis 1000: 2 wallclock secs ( 2.33 usr + 0.00 sys = 2.33 CPU) @ + 429.18/s (n=1000) With given-when: timethis 1000: 8 wallclock secs ( 7.62 usr + 0.00 sys = 7.62 CPU) @ + 131.23/s (n=1000) Test with: 0 With grep: Element 0 is in array With given-when: Element 0 is in array With grep: timethis 1000: 2 wallclock secs ( 2.17 usr + 0.00 sys = 2.17 CPU) @ + 460.83/s (n=1000) With given-when: timethis 1000: 8 wallclock secs ( 7.24 usr + 0.00 sys = 7.24 CPU) @ + 138.12/s (n=1000) Test with: 1 With grep: Element 1 is in array With given-when: Element 1 is in array With grep: timethis 1000: 2 wallclock secs ( 2.34 usr + 0.00 sys = 2.34 CPU) @ + 427.35/s (n=1000) With given-when: timethis 1000: 8 wallclock secs ( 7.44 usr + 0.00 sys = 7.44 CPU) @ + 134.41/s (n=1000) Test with: 10 With grep: Element 10 is in array With given-when: Element 10 is in array With grep: timethis 1000: 2 wallclock secs ( 2.27 usr + 0.00 sys = 2.27 CPU) @ + 440.53/s (n=1000) With given-when: timethis 1000: 8 wallclock secs ( 7.57 usr + 0.01 sys = 7.58 CPU) @ + 131.93/s (n=1000) Test with: 100 With grep: Element 100 is in array With given-when: Element 100 is in array With grep: timethis 1000: 2 wallclock secs ( 2.13 usr + 0.00 sys = 2.13 CPU) @ + 469.48/s (n=1000) With given-when: timethis 1000: 8 wallclock secs ( 7.97 usr + 0.02 sys = 7.99 CPU) @ + 125.16/s (n=1000) Test with: 1000 With grep: Element 1000 is in array With given-when: Element 1000 is in array With grep: timethis 1000: 2 wallclock secs ( 2.29 usr + 0.00 sys = 2.29 CPU) @ + 436.68/s (n=1000) With given-when: timethis 1000: 8 wallclock secs ( 7.69 usr + 0.01 sys = 7.70 CPU) @ + 129.87/s (n=1000) Test with: 10000 With grep: Element 10000 is in array With given-when: Element 10000 is in array With grep: timethis 1000: 2 wallclock secs ( 2.05 usr + 0.00 sys = 2.05 CPU) @ + 487.80/s (n=1000) With given-when: timethis 1000: 10 wallclock secs ( 9.23 usr + 0.01 sys = 9.24 CPU) @ + 108.23/s (n=1000) Test with: 10001 With grep: Element 10001 is not in array With given-when: Element 10001 is not in array With grep: timethis 1000: 2 wallclock secs ( 2.29 usr + 0.00 sys = 2.29 CPU) @ + 436.68/s (n=1000) With given-when: timethis 1000: 9 wallclock secs ( 8.54 usr + 0.01 sys = 8.55 CPU) @ + 116.96/s (n=1000) Test with: abcd With grep: Element abcd is in array With given-when: Element abcd is in array With grep: timethis 1000: 2 wallclock secs ( 2.28 usr + 0.00 sys = 2.28 CPU) @ + 438.60/s (n=1000) With given-when: timethis 1000: 10 wallclock secs ( 9.53 usr + 0.03 sys = 9.56 CPU) @ + 104.60/s (n=1000)
Bye.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: given-when construct unexpected bahavior wit arrays
by brx (Pilgrim) on Jun 08, 2012 at 07:33 UTC | |
Re^4: given-when construct unexpected bahavior wit arrays
by Tanktalus (Canon) on Jun 08, 2012 at 23:10 UTC |
In Section
Seekers of Perl Wisdom