I've benchmarked that for some huge arrays in the past ... and not detected any advantage in passing by reference (with Inline::C).

I'm guessing that you were populating the passed-by-reference Av with new SVs?

This (rnd64i( int n, SV *avref ) (i for inplace--terrible name)), assumes a pre-populated (not just pre-sized) array of n scalars into which it then sets the values to be returned. The Perl code contrasts ARGV[0] calls for 1 million values each time, with the same for rand64() which returns them on the stack and assigns them to an array.

#! perl -slw use strict; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'END_C', NAME => 'monkeys', CLEAN_AFTER_BUILD => 0; void rnd64( int n ) { dXSARGS; static unsigned __int64 y = 88172645463325252i64; SP = MARK; EXTEND( SP, n ); while( n-- ) { y ^= y << 13; y ^= y >> 7; y ^= y << 17; mPUSHu( y ); } PUTBACK; return; } void rnd64i( int n, AV* av ) { dXSARGS; SV **ary = AvARRAY( av ); static unsigned __int64 y = 88172645463325252i64; SP = MARK; while( n-- ) { y ^= y << 13; y ^= y >> 7; y ^= y << 17; sv_setuv( ary[ n ], y ); } PUTBACK; return; } END_C use Data::Dump qw[ pp ]; use Devel::Peek; use Time::HiRes qw[ time ]; my $start = time; my @rands = (1) x 1e6; for( 1 .. $ARGV[0] ) { @rands = rnd64( 1e6 ); } my $stop = time; printf "stack->array assign: Rate: %.9f\n", ( $stop - $start ) / ( 1 +e6 * $ARGV[ 0 ] ); $start = time; my @rands2 = (1) x 1e6; for( 1 .. $ARGV[0] ) { @rands = rnd64i( 1e6, \@rands2 ); } $stop = time; printf "Modify array in-place: Rate: %.9f\n", ( $stop - $start ) / ( 1 +e6 * $ARGV[ 0 ] ); exit;

The result is rnd64i() is up to 100 times faster:

C:\test>Monkeys 1 stack->array assign: Rate: 0.000011359 Modify array in-place: Rate: 0.000000121 C:\test>Monkeys 10 stack->array assign: Rate: 0.000001353 Modify array in-place: Rate: 0.000000037 C:\test>Monkeys 100 stack->array assign: Rate: 0.000000365 Modify array in-place: Rate: 0.000000030

That's a saving worth having.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^6: XS: EXTEND/mPUSHi by BrowserUk
in thread XS: EXTEND/mPUSHi by BrowserUk

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.