http://qs1969.pair.com?node_id=1216694


in reply to Inline::C on Windows: how to improve performance of compiled code?

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

Replies are listed 'Best First'.
Re^2: Inline::C on Windows: how to improve performance of compiled code?
by vr (Curate) on Jun 15, 2018 at 06:43 UTC
    # 18.2613549232483 # 3.09944152832031e-006 # 7.17122101783752 # 0.186490774154663

    Those are results of running your example (with 1e8 iterations) on Windows and Linux, respectively. Looks to me, "C from C" on Windows got optimized away, but "C from Perl" gives same picture (lagging behind, W vs L) as in OP. And right now I'm interested in eliminating this lagging. Optimization to try to avoid 1e8 calls is in future plans ;).

      Looks to me, "C from C" on Windows got optimized away

      I don't think so. (Could be wrong but.)
      A clearer ilustration is (hopefully) this script:
      use Time::HiRes qw(time); use Inline C => Config => #OPTIMIZE => '-O0', FORCE_BUILD => 1; use Inline C => <<'EOC'; void foo() {} void foo_bar(int x) { int i; for(i = 0; i < x; i++){ foo(); } } EOC $iterations = 10000000; $t = time; foo() for 1 .. $iterations; print "# ", time - $t, "\n"; $t = time; foo_bar($iterations); print "# ", time - $t, "\n";
      As it stands, with optimization enabled, it outputs (on Windows):
      # 1.02960205078125 # 1.00135803222656e-005
      Now that second value does look like something was optimized away. I'm thinking the loop is simply doing nothing at each iteration.
      When we switch optimization off by including the "OPTIMIZE => '-O0'" line, the output changes to (on Windows):
      # 1.10760188102722 # 0.0196361541748047
      The "C from C" code now takes 500 times longer to execute - because, I think, this time foo() is actually being called at each iteration. But it's still 50 times quicker than calling "C from Perl".

      I've no useful ideas regarding things that can be done to enable Windows to access C subs as quickly as it can access Perl subs - and that's the main reason that I'm avoiding that aspect.

      Cheers,
      Rob