[英]Which has better performance: executing a Perl script from C++ via system call, or calling a DLL file?
I am new to C++ coding. 我是C ++编码的新手。
I wanted to execute a Perl script (that controls an equipment) inside a C++ Visual Studio Win32 application. 我想在C ++ Visual Studio Win32应用程序中执行Perl脚本(控制设备)。 I was wondering what would be the most optimized approach in this regards?
我想知道在这方面最优化的方法是什么?
Option 2 I have never used, but I am willing to learn and implement if that's the best way. 选项2我从未使用过,但如果这是最好的方法,我愿意学习和实施。
I believe the "most optimized" approach is to embed Perl in your application. 我相信“最优化”的方法是将Perl嵌入到您的应用程序中。
Your second option is not really feasible, AFAIK. 你的第二种选择并不可行,AFAIK。 Since Perl is not compiled, your only other option would be to invoke the interpreter (eg through a
system()
or exec()
-family call), as per your first option. 由于Perl未编译,因此根据您的第一个选项,您唯一的另一个选择是调用解释器(例如通过
system()
或exec()
家庭调用)。
Now, there is quite a difference between embedding an interpreter in your code, and writing one line of code to call an external program, so you have to consider exactly how much optimization you need. 现在,在代码中嵌入解释器和编写一行代码来调用外部程序之间存在很大差异,因此您必须准确考虑所需的优化程度。 The performance difference might even be minimal, so I strongly suggest that you try the easy way first, measuring your success.
性能差异甚至可能很小,所以我强烈建议您首先尝试简单的方法,衡量您的成功。
I agree with Oystein that you're not likely to find a good way to compile the perl script into a shared library. 我同意Oystein的说法,你不太可能找到一种将perl脚本编译成共享库的好方法。
However, spawning perl from your C++ program isn't the only other option. 但是,从C ++程序中生成perl并不是唯一的选择。 You could also pipe data between the two programs, use a socket to pass data between them, and other IPC methods.
您还可以在两个程序之间管道数据,使用套接字在它们之间传递数据,以及其他IPC方法。
In my backup tool I once tried invoking system("perl script.pl")
, but it ran very slow on Windows (20-30 calls per second or so even though script.pl in a test benchmark did nothing). 在我的备份工具中,我曾尝试调用
system("perl script.pl")
,但它在Windows上运行速度非常慢(即使测试基准测试中的script.pl没有做任何事情,每秒20-30次调用左右)。 On Linux it was much, much quicker. 在Linux上它更快,更快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.