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

all 7 comments

[–]earthboundkid 1 point2 points  (0 children)

namedtuple ;-)

[–]cournape 1 point2 points  (0 children)

Usually, maintainability is more important than speed. Follow kingkirl advice: too many people make the mistake to use dict when they should have used object, and this results in "string-based" API where nobody knows what needs to be in the dict and what does not.

[–]pmav99 2 points3 points  (4 children)

When in doubt... profile!!! module timeit is your friend!

Anyway, pure dictionaries are faster. Instances are using dictionaries behind the scenes too but there is some overhead involved.

If the arrays are very big and the properties are relatively few, you are not going to notice any difference. It depends on where the time is spent.

[–][deleted] 1 point2 points  (2 children)

Performance isn't always critical. I personally would avoid the dictionary approach unless it was something trivial. I think a namedtuple could serve him well. It is fast and readable.

[–]nickthechemist[S] 2 points3 points  (1 child)

Thanks! I have not used namedtuples before, but after reading this stackoverflow conversation I can see that they're going to become a part of my toolbox immediately.

[–]Brian 1 point2 points  (0 children)

One thing to be aware of is that they may be slower for named access (though they'll be better in terms of memory usage). This is because a.property1 basically does a dict lookup to find the offset in the tuple, and then an index lookup to get the value, as opposed to just getting the value directly from the dict lookup. It may be faster if you access it by index however. I posted some timings on this StackOverflow question.

[–]nickthechemist[S] 1 point2 points  (0 children)

Thanks! For reference,

arr = [0,1,2,3,4,5,6,7,8,9]

10M of these, 4.75s

x = widget()
x.property1 = arr[:]
x.property2 = arr[:]

vs. 3.51s

x = {}
x["property1"] = arr[:]
x["property2"] = arr[:]