简体   繁体   English

为什么我的 python 函数比 c++ 中的函数运行得更快?

[英]Why does my python function run faster than the one in c++?

I have been writing a simple test to compare the speed improvements of c++ over python.我一直在编写一个简单的测试来比较 c++ 相对于 python 的速度改进。 My results were unexpected with c++ being almost trice as slow as the program written in python.我的结果出乎意料,因为 c++ 的速度几乎与用 python 编写的程序一样慢。 I guess there is something in the loop inside the python function using iterators or something.我猜python函数内部的循环中有一些东西使用迭代器或其他东西。

C++ code C++ 代码

#include <ctime>
#include <chrono>
using namespace std;
using namespace chrono;

void mult(int* a, int b)
{
    for (size_t i = 0; i < 100000000; i++)
    {
        a[i] *= b;
    }
}

int main()
{
    srand(time(0));
    int* a = new int[100000000];
    int l = 100000000;
    for (int i = 0; i < l; ++i)
    {
        a[i] = rand();
    }
    auto start = high_resolution_clock::now();
    mult(a, 5);
    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<milliseconds>(stop - start);
    cout << duration.count() << endl;
    delete[] a;
}

Python code Python代码

import time

def mult(x, a):
    for i in [x]:
        i *= a

x = np.random.random(100000000)
start = time.time() 
mult(x, 5)
elapsed = time.time()
elapsed = elapsed - start
print ("Time spent in (mult) is: ", elapsed)

Results结果

c++: 200 milliseconds c++: 200 毫秒

Python: 65 milliseconds Python:65 毫秒

There are many reasons why this performance test does not give useful results.这个性能测试没有给出有用结果的原因有很多。

  1. Don't compare, or pay attention to, release timing.不要比较或关注release时间。 The entire point of using a language like C or C++ is to enable (static) compiler optimizations.使用像 C 或 C++ 这样的语言的全部目的是启用(静态)编译器优化。 So really, the results are the same.所以真的,结果是一样的。 On the other hand, it is important to make sure that aggressive compiler optimizations don't optimize out your entire test (due to the result of computation going unused, or due to undefined behaviour anywhere in your program, or due to the compiler assuming that part of the code can't actually be reached because it there would be undefined behaviour if it were reached).另一方面,重要的是要确保积极的编译器优化不会优化整个测试(由于计算结果未使用,或者由于程序中任何地方的未定义行为,或者由于编译器假设部分代码实际上无法到达,因为如果到达它会有未定义的行为)。

  2. for i in [x]: is a pointless loop: it creates a Python list of one element, and iterates once. for i in [x]:是一个毫无意义的循环:它创建一个包含一个元素的 Python 列表,并迭代一次。 That one iteration does i *= a , ie, it multiplies i , which is the Numpy array .那一次迭代执行i *= a ,即它乘以i ,即Numpy 数组 The code only works accidentally ;该代码只能意外工作 it happens that Numpy arrays specially define * to do a loop and multiply each element.碰巧 Numpy 数组专门定义*来执行循环并乘以每个元素。 Which brings us to...这让我们...

  3. The entire point of using Numpy is that it optimizes number-crunching by using code written in C behind the scenes to implement algorithms and data structures.使用 Numpy 的全部意义在于,它通过在幕后使用用 C 编写的代码来实现算法和数据结构来优化数字运算。 i simply contains a pointer to a memory allocation that looks essentially the same as the one the C program uses, and i *= a does a few O(1) checks and then sets up and executes a loop that looks essentially the same as the one in the C code. i只包含一个指向内存分配的指针,该指针看起来与 C 程序使用的内存分配基本相同,并且i *= a进行了一些 O(1) 检查,然后设置并执行一个看起来与 C 程序基本相同的循环C代码中的一个。

  4. This is not reliable timing methodology, in general.一般来说,这不是可靠的计时方法。 That is a whole other kettle of fish.那是另一锅鱼。 The Python standard library includes a timeit module intended to make timing easier and help avoid some of the more basic traps. Python 标准库包含一个timeit模块,旨在简化计时并帮助避免一些更基本的陷阱。 But doing this properly in general is a research topic beyond the scope of a Stack Overflow question.但总的来说,正确地做到这一点是一个超出 Stack Overflow 问题范围的研究课题。


"But I want to see the slow performance of native Python, rather than Numpy's optimized stuff - " “但我希望看到原生 Python 的缓慢性能,而不是 Numpy 优化的东西——”

If you just want to see the slow performance of Python iteration , then you need for the loop to actually iterate over the elements of the array (and write them back):如果您只想查看 Python迭代的缓慢性能,那么您需要循环实际迭代数组的元素(并将它们写回):

def mult(x, a):
    for i in range(len(x)):
        x[i] *= a

Except that experienced Pythonistas won't write the code that way, because range(len( is ugly. The Pythonic approach is to create a new list:除了有经验的 Pythonistas 不会那样写代码,因为range(len(很难看。Pythonic 的方法是创建一个新列表:

def mult(x, a):
    return [i*a for i in x]

That will also show you the inefficiency of native Python data structures (we need to create a new list, which contains pointers to int objects).这也将向您展示原生 Python数据结构的低效率(我们需要创建一个新列表,其中包含指向int对象的指针)。

On my machine, it is actually even slower to process the Numpy array this way than a native Python list.在我的机器上,以这种方式处理 Numpy 数组实际上比原生 Python 列表还要慢。 This is presumably because of the extra work that has to be done to interface the Numpy code with native Python, and "box" the raw integer data into int objects.这大概是因为必须做额外的工作才能将 Numpy 代码与本机 Python 接口,并将原始整数数据“装箱”到int对象中。

Your python code does not the same thing.你的 python 代码不一样。 It does not iterate all elements of x , it rather iterate over list (of size 1) containing 1 element: the x (list).它不会迭代x的所有元素,而是迭代包含 1 个元素的列表(大小为 1): x (列表)。 List of one list.一个列表的列表。

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

相关问题 为什么Python运行C ++函数比通过main()函数运行自己的函数的C ++更快? - Why does Python run a C++ function faster than C++ running its own function via its main() function? 为什么我的python 3实现比我用C ++编写的实现快得多? - Why is my python 3 implementation much faster than the one I wrote in C++? 为什么我的 Python NumPy 代码比 C++ 快? - Why is my Python NumPy code faster than C++? 为什么我的程序比使用内置函数的python快? - Why is my program faster than the one using a python built in function? 在这种情况下,为什么Python比C ++更快? - Why is Python faster than C++ in this case? 为什么Python比C ++排序更快 - Why is Python sort faster than that of C++ 为什么 Python 代码在函数中运行得更快? - Why does Python code run faster in a function? 为什么线性搜索比Python3中的二进制搜索运行得更快? - Why does my linear search run faster than my binary search in Python3? 为什么我的代码在Eclipse控制台中比在Eclipse的PyDev Python控制台中运行得更快? - Why does my code run faster in Eclipse console than in Eclipse's PyDev Python Console? 为什么此算法在python中比在C ++中工作得这么快? - Why does this algorithm work so much faster in python than in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM