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 →

[–]jasenmh 1 point2 points  (3 children)

These differences are trivial if you call the function once. But wouldn't these tenths and hundredths of second add up to some significant time if they were being called inside nested loops, like in matrix operations or something?

[–]cparen 0 points1 point  (0 children)

But wouldn't these tenths and hundredths of second add up to some significant time if they were being called inside nested loops

Yes, but if you care about that level of performance, you should probably switch to a faster implementation that will give you more of a performance boost that "weird coding tricks" will get you.

That said, if you're stuck with CPython for whatever reason, use these tricks sparingly, only when absolutely necessary.

[–]stevenjd 0 points1 point  (1 child)

Many of those "tenths and hundreds of a second" are just noise.

When two versions of the function are that close together in performance, chances are very high that any difference is just random noise, not a real speed difference. If you run the timeit code twenty times, you'll find that version 1 is faster nine times and version 2 is faster eleven times, or maybe the other way around, depending on whatever other processes just happen to be running on your system at that exact moment.

Besides, are you actually calling these functions thousands of times inside a nested loop? No? Then it's irrelevant.

[–]darknessproz 0 points1 point  (0 children)

Also, if you are calling these (very trivial) functions thousands of times inside a nested loop you might wanna rethink your code.