3 comments

[ 3.2 ms ] story [ 12.0 ms ] thread
"Python haters always say, that one of the reasons they don’t want to use it, is that it’s slow."

There are no critics nowadays, only "haters". This word usage irks me. Someone who does not use Python because he thinks it is too slow does not "hate" it.

Fair enough, I guess that's bad wording. Critic is better word here.
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.