Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

calling perl from C [performance optimization]

by jithoosin (Scribe)
on Dec 15, 2005 at 07:29 UTC ( #516881=perlquestion: print w/replies, xml ) Need Help??

jithoosin has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,
I doing a project using perl & C. I always use "system" command in C to call perl files and use "GetOptions" in perl files to get those arguments passed from C . That is data is passed between C and perl using pipes ,"popen" command in C. My friends are saying that using perl is slow and moreover the use of pipes make it even slower.

The problem is that i have to call perl files near 40000 times from C during execution and in each of those calls i have to pass entire html pages(text only no images) from C file to perl files using pipes.

I don't believe in converting perl files to C code or to use mod-perl. Is there any other go to increase the speed (eg: like replacing the "system" & "popen" commands by faster alternatives]
  • Comment on calling perl from C [performance optimization]

Replies are listed 'Best First'.
Re: calling perl from C [performance optimization]
by ikegami (Patriarch) on Dec 15, 2005 at 07:39 UTC

    1) Why don't you embed the Perl "interpreter" in the C application? See perlembed

    2) Alternatively, it would surely be faster to launch a single Perl process and send the 40000 requests to it (via a pipe or whatever), rather than launching 40000 Perl processes (which requires compiling the script and the modules it uses every time).

    3) Yet another possibility is to have the C program output a Perl program. This is particulary useful when each call to Perl is independant of the others. For example, the Perl script might look like:

    use Guts; guts(value_calulated_by_C_1); guts(value_calulated_by_C_2); ... guts(value_calulated_by_C_40000);

    After running this one Perl script, the C program could process the output if need be.

      Elaboration on #3, by request:

      If your script normally looks like

      $value = ...from @ARGV from C program... guts($value);

      then I'm simply suggesting the C program could create a .pl that calls guts multiple times with the appropriate values hardcoded.

Re: calling perl from C [performance optimization]
by Courage (Parson) on Dec 15, 2005 at 09:23 UTC
    you should certainly read perldoc perlipc, which comes with Perl distribution. Very useful reading, from many points of view.

    Also, as FAQ says, use Inline::C; from CPAN should fit, it will give you a breakthrough speed boost with a little efforts.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://516881]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2023-02-06 07:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (33 votes). Check out past polls.

    Notices?