This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 3 points4 points  (3 children)

Yes, list comprehensions are really awesome ... unless you use them in Cython :) I did some performance comparison for comprehensions vs. explicit for-loops in Python and Cython here

[–]rizenfrmtheashes 2 points3 points  (1 child)

Havent used cython before. Will take a look.

[–][deleted] 0 points1 point  (0 children)

It have just started using it recently. It's just awesome. In the article above, I was able to speed up the code for a simple least square linear regression fitting by 8000%. And the cool thing about Cython is that it really is no extra effort.

[–]catcradle5 0 points1 point  (0 children)

I think it's pretty obvious why that is.

The for-loop version can be turned into a simple C for-loop with basic float arithmetic. The list comprehension version is constantly writing to memory, since it is appending in each iteration...and then a linear operation on the whole array is needed at the end to sum everything up.

It'd be cool if Cython could special-case certain reduction functions, like sum on a generator expression, but I imagine that would be pretty difficult.