0

I write perl code that calls a submodule tens of millions times. The submodule is computationally intensive and its runtime is very slow. I find a C++ program that exactly does what the submodule does and I would like to substitute the submodule with the C++ program. I am wondering whether I have to write XS code to interface the program. Does it decrease the performance a lot to directly call the C++ program using the "system" command in the perl code? Thanks!

  • check: https://metacpan.org/pod/Inline::CPP – clt60 Feb 03 '15 at 07:22
  • @jm666 Thank you for your comments. The module seems to decrease the effort for embedding C code into perl, and it would be useful if I have to use XS. However, I cannot find the answer for my question there... – user4147776 Feb 03 '15 at 07:52

1 Answers1

1

Launching an external program is is always going to be slower than making a function call. If you care about speed, launching a program "tens of millions of times" is out of the question.

If the loop which is executed tens of millions of times is inside the external program, then launching it only once may be acceptable. However, you now have another problem: how to get tens of millions of data to the external program and how to get the results back. Because it's an external program, you'll have to pass the data in text form. This means your script has to transform the data into text, pass it to the external program; the external program has to parse it and transform it into its native representation, perform the calculations, transform the results into text and return it; then your script has to parse the result.

system is probably not the right tool for this anyway. It is best suited for running programs for their effect (e.g. rm -rf /) rather than their output. If you want to read the output of a program, you probably want backticks (`` a.k.a. qx{}) or piping to yourself (see "Using open() for IPC" in perldoc perlipc).

Bulletmagnet
  • 5,665
  • 2
  • 26
  • 56
  • If the external program is interactive, and the loop is in the perl code, using `open` once before the loop and passing instructions/data on each iteration is worth considering as well. Am I right? – rubikonx9 Feb 03 '15 at 08:49
  • That _is_ worth considering, but two-way communication is problematic. See "Bidirectional Communication with Another Process" in `perldoc perlipc` – Bulletmagnet Feb 03 '15 at 09:00
  • @Bulletmagnet Thank you for your answer. I am sorry that I cannot vote up your answer because I don't have enough reputation. – user4147776 Feb 03 '15 at 09:33
  • FYI. I wrote two simple perl programs: one calls an empty (doing nothing) C++ program 10,000 times using backticks, and the other uses xs to call the C++ program 10,000 times. The runtime of the backtick version is 14.44 s and that of the xs version is 0.02 s. There is a huge difference. – user4147776 Feb 03 '15 at 21:59