Alcott
Alcott

Reputation: 18595

performance concern, Python vs C

I was using PIL to do image processing, and I tried to convert a color image into a grayscale one, so I wrote a Python function to do that, meanwhile I know PIL already provides a convert function to this.

But the version I wrote in Python takes about 2 seconds to finish the grayscaling, while PIL's convert almost instantly. So I read the PIL code, figured out that the algorithm I wrote is pretty much the same, but PIL's convert is written in C or C++.

So is this the problem making the performance's different?

Upvotes: 0

Views: 334

Answers (2)

HYRY
HYRY

Reputation: 97331

If you want to do image processing, you can use

OpenCV(cv2), SimpleCV, NumPy, SciPy, Cython, Numba ...

OpenCV, SimpleCV SciPy have many image processing routines already.

NumPy can do operations on arrays in c speed.

If you want loops in Python, you can use Cython to compile your python code with static declaration into an external module.

Or you can use Numba to do JIT convert, it can convert your python code into machine binary code, and will give you near c speed.

Upvotes: 2

Ned Batchelder
Ned Batchelder

Reputation: 375932

Yes, coding the same algorithm in Python and in C, the C implementation will be faster. This is definitely true for the usual Python interpreter, known as CPython. Another implementation, PyPy, uses a JIT, and so can achieve impressive speeds, sometimes as fast as a C implementation. But running under CPython, the Python will be slower.

Upvotes: 3

Related Questions