-1

Tried running the below code using Inline C, the speed was 10reqs/s compared with pure perl hello world at 110reqs/s. What's the problem?

#!/usr/bin/perl -w
use Inline (Config => DIRECTORY => '/home/example/.Inline',);
use Inline 'C';


greet();
exit 200;


__END__

__C__

void greet() {
printf("Hello, world\n");
}
stashfree
  • 655
  • 6
  • 22
  • Calling a C function to call Perl's printf shouldn't be any faster than calling a Perl operator written in C to call Perl's printf. The difference is library loading overhead. – ikegami Nov 30 '14 at 06:03

1 Answers1

2

Because your script doesn't do anything useful. Even ignoring the first run (where it has to compile the C source), the amount of work that Inline::C does to checksum the C source, check that it's already been compiled and dynamically link the function is way more work than just printing "Hello world". If you compared a Perl version and an Inline::C version of a function that did some fairly expensive numeric computation, you would see a very different result.

hobbs
  • 223,387
  • 19
  • 210
  • 288
  • but 10x slower... it was already running multiple times so it's already compiled... I get what you mean... Inline::C ... is slower... too slow in fact for web purposes. Any ways to remove the checking and just used the latest compiled version? – stashfree Nov 30 '14 at 03:45
  • 1
    @stashfree 10x slower **at doing nothing**. The measurement is meaningless and your conclusion is completely wrong. – hobbs Nov 30 '14 at 03:47
  • i can't even do a proper speedup in c by printing some text? – stashfree Nov 30 '14 at 03:49
  • 1
    @stashfree why would C be faster at printing text? `print` costs basically nothing, almost all of the time in your script is startup cost, not "printing text". Using Inline::C gives you more startup cost. – hobbs Nov 30 '14 at 03:50
  • 2
    @stashfree If you want a speed boost from using C you need to do something that C does faster than Perl, for starters. And you need to do *enough* of it to outweigh the overhead. A single print fails both tests. – hobbs Nov 30 '14 at 03:53
  • I get it. Thanks... so either the script should be running as a daemon or a loop without perl re-calling Inline C, then things will be different. Right? Guess that's it. – stashfree Nov 30 '14 at 11:32