r/Python • python-programming.courses • Oct 30 '15

Improving your code readability with namedtuples

https://python-programming.courses/pythonic/improving-your-code-readability-with-namedtuples/
186 Upvotes

79 comments sorted by

View all comments

Show parent comments

2

u/are595 Oct 31 '15

Wow, I never knew about __slots__. I just shaved 14% total run time off of a script I was writing that has to deal with a lot of objects (on the order of hundreds of thousands)! I didn't check memory usage, but I'm sure that went down as well.

Is there any place good for learning about these kinds of performance tips?

1

u/d4rch0n Pythonistamancer Oct 31 '15

Ha, nice catch! That's exactly the sort of case where it can help.

I haven't ran into any sites, but I think the best thing you could be doing is running cProfile if you don't already. Profiling your code is key. There's not much point to increase the performance of function foo if your code spends .1% of its time in there, and spends 20% of its time in bar. You can't know that without profiling (or some intense manual analysis).

Another thing you might look into is PyPy. People don't use that nearly as much as they could. Unless you use all the newest features of python 3.x, pypy is likely compatible with your code. If you have long-running scripts where the JIT can get warmed up, you can get huge performance increases sometimes. I had scripts that ran in the order of five minutes, and simply switching to pypy dropped it down to about half that. I experimented with some other date conversion thing and it dropped it to a quarter of the time.

And that is just a change in the environment, not the code base.

Here, just found this:

https://wiki.python.org/moin/PythonSpeed/PerformanceTips

I'll have to go through that a few times. Some great info there.

0

u/Daenyth Oct 31 '15

Google for profiling