简体   繁体   English

为什么Python运行C ++函数比通过main()函数运行自己的函数的C ++更快?

[英]Why does Python run a C++ function faster than C++ running its own function via its main() function?

I wrote an extremely brute force function to check if a number is a prime number. 我写了一个非常强力的函数来检查数字是否是素数。 The loop goes up to 1,000,000. 循环次数达到1,000,000。 I compiled that C++ code into a shared library and ran that function with Python, then I ran the same function within C++'s main() function. 我将C ++代码编译成共享库并使用Python运行该函数,然后在C ++的main()函数中运行相同的函数。 Why does time show that python did it faster than C++? 为什么时间表明python比C ++更快?

My C++ Code (cppcode.cpp): 我的C ++代码(cppcode.cpp):

#include <iostream>
#include <boost/python.hpp>
using namespace boost::python;

bool isPrime(long number) {
    if(number == 2) {
        return true;
    }
    for(int i = 3; i < number; i++) {
        if(number % i == 0) {
            return false;
        }
    }
    return true;
}

void runcpp(void) {
    for(int i = 2; i < 1000000; i++) {
        if(isPrime(i)) {
            std::cout << i << " is a prime number!" << std::endl;
        }
    }
}

BOOST_PYTHON_MODULE(cppcode)
{
    def("runcpp", runcpp);
}

int main() {
    runcpp();
    return 0;
}

My Python Code (main.py): 我的Python代码(main.py):

import cppcode
if __name__ == "__main__":
    cppcode.runcpp()

Shell Output: 壳输出:

$ g++ -Wall -shared -fPIC cppcode.cpp -o cppcode.so -lpython2.7 -lboost_python -I/usr/include/python2.7/
$ g++ -Wall cppcode.cpp -o main -lpython2.7 -lboost_python -I/usr/include/python2.7/
$ (time python main.py >> time.txt) && (time ./main >> time.txt)

real    10m26.519s
user    10m25.042s
sys     0m0.737s

real    10m48.754s
user    10m47.796s
sys     0m0.763s

As you can see from the shell output above, when python ran the C++ "runcpp" function, it ran ~20 seconds faster than when C++'s main() function ran the same "runcpp" function. 从上面的shell输出可以看出,当python运行C ++“runcpp”函数时,它比C ++的main()函数运行相同的“runcpp”函数时运行快约20秒。 Does anyone have any idea as to why Python executed the same function faster than C++? 有没有人知道为什么Python比C ++更快地执行相同的功能? Or am I reading it wrong? 或者我读错了?

Remove all print statements - these cause the program to pause and you're benchmarking the time your system spends doing I/O which is highly variable and blankets any differences between your Python and your pure C++ runtime. 删除所有打印语句 - 这些会导致程序暂停,并且您正在对系统花费在I / O上的时间进行基准测试,I / O是高度可变的,并且覆盖了Python和纯C ++运行时之间的任何差异。

Try doing some heavy mathematical computation like finding the first 1000 primes for a more fair comparison. 尝试做一些重要的数学计算,比如寻找前1000个素数以进行更公平的比较。

Having said that, I don't expect your C++ program to outperform your Python program by much if at all. 话虽如此,我不希望你的C ++程序比你的Python程序更好。 They should be neck-to-neck for the most part with the only possible disadvantage for Python being its interpreter's "boot-up" time. 在大多数情况下,它们应该是颈部到颈部,唯一可能的缺点是Python是其解释器的“启动”时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM