note
syphilis
<I>i hope for hints of what could be changed</I><br><br>
The first hint that comes to mind is to perform the 1e8 function calls to foo() from inside C space, rather than from Perl space.
<br>On top of the significant reduction in overhead, one then might also get to take advantage of C optimizations that are lost when the C function is called from Perl.
<br>The following script aims at demonstrating the sort of savings you might get. I've changed foo() to be a little bit more than a no-op, in the hopes that it will remove the effect of clever C optimizations. (I don't know if I've been successful.):
<c>
use Time::HiRes qw(time);
use Inline C => <<'EOC';
int foo(int x) {return x + 1;}
int foo_bar(int x) {
int i;
for(i = 0; i < x; i++){
foo(i);
}
return x + i;
}
EOC
$iterations = 10000000; # 1e7
$t = time;
foo($_) for 1 .. $iterations;
print "# ", time - $t, "\n";
$t = time;
foo_bar($iterations);
print "# ", time - $t, "\n";
# Outputs (on Windows):
# 1.57560181617737
# 0.0026400089263916
</c>
On my Ubuntu (16.04) box, running perl-5.26, the same script outputs:
<c>
# 1.80176305770874
# 0.0337138175964355
</c>
Cheers,
<br>Rob
1216682
1216682