i hope for hints of what could be changed
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.
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.
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.):
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
On my Ubuntu (16.04) box, running perl-5.26, the same script outputs:
# 1.80176305770874
# 0.0337138175964355
Cheers,
Rob
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|