Optimizing this function via Inline.pm really interested me, so I did a few more benchmarks. What I found quite unexpected was that sort actually came out ahead of the Inline-C version for very small arrays. Is there substantial overhead in XS that isn't present in built-ins?

update: The c_minmax Inline sub had a silly inefficiency, which was fixed. Now sort is slower, by about 5-10%. Benchmarks have been updated to reflect this. Hurray?

# keywords: bench hash clobber override use strict; use warnings; use Benchmark qw(cmpthese); use Data::Dumper; $| = 1; sub shuffle { my $array = shift; for (my $i = @$array; --$i; ) { my $j = int rand ($i+1); @$array[$i,$j] = @$array[$j,$i]; } } sub iter_minmax { my $min = my $max = shift; for (@_) { if ($max < $_) { $max = $_ } elsif ($min > $_) { $min = $_ } } return ($min, $max); } sub sort_minmax { (sort { $a <=> $b } @_)[0,-1]; } use Inline C => <<'END_OF_C_CODE'; void c_minmax(SV* array1, ...) { Inline_Stack_Vars; int max = SvIV(Inline_Stack_Item(0)); int min = max; int i, val; for (i = 1; i < Inline_Stack_Items; i++) { val = SvIV(Inline_Stack_Item(i)); if (val > max) max = val; else if (val < min) min = val; } Inline_Stack_Reset; Inline_Stack_Push(sv_2mortal(newSViv(min))); Inline_Stack_Push(sv_2mortal(newSViv(max))); Inline_Stack_Done; } END_OF_C_CODE for my $size (10,100,1000,10000) { my %hash; my @array = 1..$size; shuffle(\@array); print "\n\n--- A hash of size $size ---\n"; cmpthese (-3, { c => sub { c_minmax(@array) }, iter => sub { iter_minmax(@array) }, sort => sub { sort_minmax(@array) }, }); } ### RESULTS ### --- Array of size 10 --- Rate iter sort c iter 81433/s -- -69% -72% sort 260421/s 220% -- -10% c 288754/s 255% 11% -- --- Array of size 100 --- Rate iter sort c iter 12174/s -- -44% -85% sort 21817/s 79% -- -73% c 81029/s 566% 271% -- --- Array of size 1000 --- Rate iter sort c iter 1298/s -- -10% -87% sort 1448/s 12% -- -85% c 9976/s 669% 589% -- --- Array of size 10000 --- Rate sort iter c sort 59.3/s -- -45% -94% iter 108/s 82% -- -89% c 953/s 1509% 782% --

In reply to Re: Returning the lowest key in a hash (or highest) by MeowChow
in thread Returning the lowest key in a hash (or highest) by deprecated

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.