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


in reply to Re: Choosing the right sort (Or: WUDsamatter?)
in thread Choosing the right sort (Or: WUDsamatter?)

Just to clarify Pustular Postulant, the OP must have called the functions in a void or scalar context. sort is optimized to do nothing in that situation, so the functions naive and orcish return instantly. Read more for confirmation of Pustular Postulant's results and confirmation that running the sorts in scalar context give results similar to those of the OP.

my @unsorted = sort map { "foo$_.tla" } 0..999; Benchmark::cmpthese(5000, { naive => sub { my @a = naive @unsorted; }, orcish => sub { my @a = orcish @unsorted; }, schwartzian => sub { my @a = schwartzian @unsorted; }, guttros => sub { my @a = guttros @unsorted; }, });

gives me

Rate naive orcish schwartzian guttros naive 48.3/s -- -34% -46% -60% orcish 73.1/s 51% -- -19% -40% schwartzian 90.1/s 86% 23% -- -26% guttros 122/s 152% 67% 35% --

while

my @unsorted = sort map { "foo$_.tla" } 0..999; Benchmark::cmpthese(5000, { naive => sub { scalar naive @unsorted; }, orcish => sub { scalar orcish @unsorted; }, schwartzian => sub { scalar schwartzian @unsorted; }, guttros => sub { scalar guttros @unsorted; }, });

gives me

(warning: too few iterations for a reliable count) (warning: too few iterations for a reliable count) Rate schwartzian guttros orcish naive schwartzian 107/s -- -26% -100% -100% guttros 144/s 35% -- -100% -100% orcish 106383/s 99666% 73834% -- -0% naive 106383/s 99666% 73834% 0% --

That's why it's important to show one's work :)