http://www.perlmonks.org?node_id=1067574


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

I'm starting to doubt if XS is really worth it.

Beside the hard to fix bugs, these modules are not that much faster.

In my benchmarks pure Perl5.10 approaches of any were in the range of 80%-50% of the speed of List::MoreUtils !

Interesting is

Rate PP_for LU_first PP_grep PP_map PP_mapdirec +t LMU_any PP_for 323355/s -- -4% -9% -16% -18 +% -47% LU_first 335913/s 4% -- -6% -13% -15 +% -45% PP_grep 356464/s 10% 6% -- -8% -10 +% -42% PP_map 387000/s 20% 15% 9% -- -2 +% -37% PP_mapdirect 395959/s 22% 18% 11% 2% - +- -35% LMU_any 612649/s 89% 82% 72% 58% 55 +% --

Have to repeat the benchmark with more test cases...

Cheers Rolf

( addicted to the Perl Programming Language)

Replies are listed 'Best First'.
Re^9: Smartmatch alternatives
by tobyink (Canon) on Dec 18, 2013 at 07:34 UTC

    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
      *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% --