7 comments

[ 4.2 ms ] story [ 30.8 ms ] thread
The python code does much more than the supposedly equivalent C code. Why use an array for caching in C but a defaultdict in python? Especially when not even using the defaultdict functionality? Strange choice.
Have you tried that code in pypy? http://pypy.org/
yup, the code run significantly faster than before. tho it's still relatively slower than C.
Why is this piece titled "Array is slow in python?" when it uses defaultdict (a key/value mapping) instead of an array? Yes, if what you need is an array, defaultdict is a slow and cumbersome replacement, sort of like using NASA's giant crawler-transporters for your daily commute.

When you need something array-like, Python lists or arrays (the latter from the standard library array module) would be a more sensible choice. (Arrays are unidimensional, but its pretty trivial to emulate a multidimensional array with a unidimensional one.)

Agreed. Have given list a try actually, the speed is better, but still not efficient enough.
Python lists are dynamically resizable, unlike normal C arrays, and can hold arbitrary objects. They aren't going to ever be as efficient as C arrays -- while they aren't as "big" as defaultdict, they are a lot more than simple C arrays.

Python arrays should be more efficient for homogenous arrays of one of the types they are specialized for (including ints), and if the performance of the list/array is really a bottleneck in your application, there are more specialized tools available (including moving that bit off into a C extension).

There are certainly places where every bit of performance out of a collection matters, but there are more places where being able to quickly put together code that works correctly matters (for one thing, being able to quickly put together correct code makes it possible to correctly identify the bottlenecks where you need to optimize performance.)

True, and I like python list alot as it offer the ability to hold heterogenous objects and it's really flexible & simple. Just happened to using list the other day to cache a large 2d array, which turned out to be slow. Certainly, my implementation has room for improvement, and my comparison isn't straightly an apple to apple. Taken down the post n modifying. Thanks for the comment btw.