Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^9: Smartmatch alternatives

by tobyink (Abbot)
on Dec 18, 2013 at 07:34 UTC ( #1067601=note: print w/ replies, xml ) Need Help??


in reply to Re^8: Smartmatch alternatives
in thread Smartmatch alternatives

Really? In my benchmarks, the XS versions are significantly faster. The List::Util XS version is faster than List::MoreUtils too.

use strict; use warnings; use Benchmark qw(cmpthese); use List::Util 1.35 (); BEGIN { *any_lu = *List::Util::any }; use List::MoreUtils (); BEGIN { *any_lmu = *List::MoreUtils::any }; sub any_pp (&@) { my $code = shift; &$code and return 1 for @_ ; () } { no warnings 'once'; @::LIST = (0..9); $::TARGET = 5; } cmpthese(-1, { any_lu => q[ any_lu { $_ eq $::TARGET } @::LIST ], any_lmu => q[ any_lmu { $_ eq $::TARGET } @::LIST ], any_pp => q[ any_pp { $_ eq $::TARGET } @::LIST ], }); __END__ Rate any_pp any_lmu any_lu any_pp 65761/s -- -79% -80% any_lmu 309688/s 371% -- -5% any_lu 324588/s 394% 5% --

For longer lists, the difference becomes more pronounced:

{ no warnings 'once'; @::LIST = (0..99); $::TARGET = 50; } ... __END__ Rate any_pp any_lmu any_lu any_pp 10195/s -- -84% -85% any_lmu 63999/s 528% -- -4% any_lu 66370/s 551% 4% --
use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name


Comment on Re^9: Smartmatch alternatives
Select or Download Code
Replies are listed 'Best First'.
Re^10: Smartmatch alternatives
by LanX (Canon) on Dec 18, 2013 at 08:15 UTC
    *COMPLETELY UPDATED, FORGET WHAT I WROTE BEFORE IN THIS POST*

    Sorry using string benchmarks didn't combine well with lexical globals in my last benchmark.

    Your benchmark with ano-subs shows LMU being 3 times as fast for arrays of length 1000.

    use strict; use warnings; use Benchmark qw(cmpthese); use Data::Dump qw/pp/; #use List::Util 1.35 (); BEGIN { *any_lu = *List::Util::any }; use List::MoreUtils (); BEGIN { *any_lmu = *List::MoreUtils::any }; sub any_pp (&@) { my $code = shift; map {&$code and return 1} @_ ; () +} my (@LIST,$TARGET); our (@LIST2,$TARGET2); { no warnings 'once'; @LIST2 = @LIST = (0..1000); $TARGET2 = $TARGET = $LIST[@LIST/2]; } local $,="\t"; my $suite= { any_lmu => sub { any_lmu { $_ eq $TARGET } @LIST }, any_pp => sub { any_pp { $_ eq $TARGET } @LIST }, }; pp $suite; cmpthese(-1,$suite); __END__ { any_lmu => sub { "???" }, any_pp => sub { "???" } } Rate any_pp any_lmu any_pp 1027/s -- -66% any_lmu 3027/s 195% --

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1067601]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (13)
As of 2015-07-31 10:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (276 votes), past polls