----- Original Message ----- From: "Eric Wilhelm" <[EMAIL PROTECTED]>
To: <inline@perl.org>
Sent: Friday, January 19, 2007 7:51 PM
Subject: Re: Percentiles


# from Sisyphus
# on Thursday 18 January 2007 09:51 pm:

In terms of performance, I don't think it matters much which way you
go about it.

If the caller is routinely passing extremely large arrays, passing as an
array ref should be quite a bit faster than a list of values (each of
which gets copied.)

Well ... I can't detect a significant difference for a 10,000,000 element array of integers. Perhaps I've been sucked in by a benchmarking sophism. (Apologies if that's the case.) I also suspect (though I haven't checked recently), that if you look at the generated .c file, you'll see that there's buggerall difference.

Here's my test script:

-----------------------------
use warnings;
use Benchmark;

use Inline C => <<'EOC';

void by_list(SV * x, ...) {
    Inline_Stack_Vars;
    int z, i, len = Inline_Stack_Items;

    for(i = 0; i < len; i++) {
       z = SvIV(Inline_Stack_Item(i));
       //printf("%d\n", z); // to verify that values are correct
   }
}

void by_ref(AV * x) {
    int len;
    int i,z;
    SV ** elem;

    len = av_len(x) + 1;

    for(i = 0; i < len; i++) {
       z = SvIV(*(av_fetch(x, i, 0)));
       //printf("%d\n", z); // to verify that values are correct
    }
}

EOC

@array = ();
$elements = 10000000;

for (1 .. $elements) {push @array, $_}

print "DONE\n";

timethese(1, {
'by_list' => 'by_list(@array);',
'by_ref' => 'by_ref([EMAIL PROTECTED]);',
});
--------------------------------------

For me, that produces:

--------------------------------------
DONE
Benchmark: timing 1 iterations of by_list, by_ref...
by_list: 0 wallclock secs ( 0.47 usr + 0.00 sys = 0.47 CPU) @ 2.13/s (n=1
)
           (warning: too few iterations for a reliable count)
by_ref: 1 wallclock secs ( 0.45 usr + 0.00 sys = 0.45 CPU) @ 2.21/s (n=1
)
           (warning: too few iterations for a reliable count)
------------------------------------

Cheers,
Rob

Reply via email to