Reputation: 71
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!
Upvotes: 0
Views: 364
Reputation: 6010
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
).
Upvotes: 1