3 ms·
I have a few comments about some of the slides, feel free to correct any misunderstandings. Dictionary vs Object: Lookups in both data structures is O(1), the
by wting 14y ago
I have a few comments about some of the slides, feel free to correct any misunderstandings.
Dictionary vs Object:
Lookups in both data structures is O(1), the difference being the hashing cost (and an additional memory lookup for heap) vs a single memory lookup on the stack (1 line of assembly).
Squares list:
> ... so every iteration through the list we have the potential need to size the list and copy all the data.
This is no different than stl::vector which has an amortized cost of O(1) for a push_back().
It's not going to be as fast as C, but I'd also argue for a generator version instead:
def squares(n):
return (i*i for i in xrange(n))
One of the main reasons people choose Python is for expressiveness and not manually managing memory, although pre-allocation does seem like a good idea.
- kragen 14y agoYou mean std::vector, from the STL. And yes, the amortized cost is O(1) per element and thus O(N) in total, but the constant factor and lower-order terms (the O(1) time to do the allocation and garbage-collect it later) do matter.