Why is cffi so much quicker than numpy?

前端 未结 1 1051
夕颜
夕颜 2021-02-05 15:48

I have been playing around with writing cffi modules in python, and their speed is making me wonder if I\'m using standard python correctly. It\'s making me want to switch to C

相关标签:
1条回答
  • 2021-02-05 16:14

    Numpy is slower than C for two reasons: the Python overhead (probably similar to cffi) and generality. Numpy is designed to deal with arrays of arbitrary dimensions, in a bunch of different data types. Your example with cffi was made for a 2D array of floats. The cost was writing several lines of code vs .sum(), 6 characters to save less than 5 microseconds. (But of course, you already knew this). I just want to emphasize that CPU time is cheap, much cheaper than developer time.

    Now, if you want to stick to Numpy, and you want to get a better performance, your best option is to use Bottleneck. They provide a few functions optimised for 1 and 2D arrays of float and doubles, and they are blazing fast. In your case, 16 times faster, which will put execution time in 0.35, or about twice as fast as cffi.

    For other functions that bottleneck does not have, you can use Cython. It helps you write C code with a more pythonic syntax. Or, if you will, convert progressively Python into C until you are happy with the speed.

    0 讨论(0)
提交回复
热议问题