R – see a delay when I call a Perl program with system()

perl

Scenario 1:
I have one wrapper Perl script which uses another Perl module and invokes a function in that module.

Scenario 2:
Now I have the same wrapper script and the module is implemented as Perl script. Here, instead of using module I am simply calling system("perl anotherscript.pl").

Both do the same function, but I am seeing a little bit delay in the second scenario.

Why is it so? Is it expected or is it something to do with my code?

Best Answer

In the first situation, you have the overhead of the Perl interpreter. In the second, you have the overhead of two Perl interpreters. If you want the second approach, consider an alternate and little-used version of the do function (see perldoc -f do):

do './anotherscript.pl'

If the Perl interpreter is designed at all well (and it probably is), this will probably run significantly faster than the second example, though I don't know how it will compare to the first one. If you want efficiency, benchmark.

EDIT: If you don't care about the (probably insignificant) performance difference between the two, I recommend you just use a module. It will make your code infinitely more useful, because while a script can only be re-used in one piece, modules can be re-used in as many separate pieces as you like.

Related Topic