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.