"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.
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.
3 comments
[ 3.2 ms ] story [ 12.0 ms ] threadThere 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.
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:
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.