Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^6: XS: EXTEND/mPUSHi

by BrowserUk (Pope)
on Sep 27, 2011 at 07:21 UTC ( #928030=note: print w/replies, xml ) Need Help??


in reply to Re^5: XS: EXTEND/mPUSHi
in thread XS: EXTEND/mPUSHi

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.

Replies are listed 'Best First'.
Re^7: XS: EXTEND/mPUSHi
by syphilis (Chancellor) on Sep 28, 2011 at 08:24 UTC
    I'm guessing that you were populating the passed-by-reference Av with new SVs?

    Quite possibly - looking at those figures I must have been doing *something* dumb :-)

    Cheers,
    Rob

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2016-09-27 00:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:







    Results (493 votes). Check out past polls.