7 ms·
I hope I'm not teaching you to suck eggs, but you can use a profiler to do that rather than changing your code with what are basically print statements. I don'
by mattmanser 9y ago
I hope I'm not teaching you to suck eggs, but you can use a profiler to do that rather than changing your code with what are basically print statements.
I don't use Python any more and have never profiled it, but there seems to be good options:
https://docs.python.org/2/library/profile.html https://docs.python.org/2/library/profile.html
There are reasons to do what you're doing, e.g. Stack Exchange have a mini-profiler to watch production, but to identify problems in a development environment, profilers are usually quicker/easier once you've learnt to use them. It also helps you identify problems you didn't even know about.
- mherrmann 9y agoInteresting point, though again much more complex. [edit] Anyone reading this: Do not discard cProfile like I did. It just let me shave 900 ms off the startup time of my file manager (mentioned in a child comment). Wow!
- mattmanser 9y agoI admit they take time up front to learn to use and interpret the results, but it's a valuable skill and worth doing every year or so even on smaller projects to see if there are any easy wins.
- mherrmann 9y agoYes, you're right. Mine is just the simplest and probably least powerful solution. cProfile is definitely more powerful.
- rrauenza 9y agoI think yours still has a valid use in production logging...
- mherrmann 9y agoOmg, cProfile just let me shave 900ms off the startup-time of my PyQt-based file manager [1]. I would not have found this that quickly without your hint. Thank you! [1]: https://fman.io https://fman.io
- mattmanser 9y agoNo problem! This was on the front page today, but flagged as a dupe, also an interesting read as it explains about flame graphs too: https://news.ycombinator.com/item?id=14384370 https://news.ycombinator.com/item?id=14384370
- dom0 9y agoCouple notes about cProfile - The slowdown is difficult to predict and somewhat unstable; never compare timings from runs with/without cProfile - It's a deterministic, wall-clock profiler - pyprof2calltree & kcachegrind. - Loading a profile file = arbitrary code execution. Don't ever load a profile file you did not generate yourself.
- szatkus 9y agoI find SnakeViz much more simple and user-friendly.