News for hackers. Stuff that matters.
Re:Making Python Programs Blazingly Fast by eesmith on Wednesday January 01, 2020 @03:56PM (#21930569)
Should also use kernprof - https://github.com/rkern/line_profiler - to see which lines in that function take the most time.

Oddly, none of the techniques listed would help that slow_program.py function since kernprof says that 99.8% of the time is spent in:

    s += num / fact
One comment about "# Fast:" / "from re import findall" -- That's not fast. Sure, it reduces a getattr lookup, but it's much better to pre-compile the regex than to pass it in to re.findall() each time.

Now, re.findall() does cache the last 100 or so regexps, so it probably won't re-evaluate the regex each time. But really, pre-compute that regex with "_my_pattern = re.compile(regex) ... _my_pattern.findall()" and avoid even that cache lookup.

There are no comments on this comment yet.