4 ms·
Fourth time this has been posted in 12 days. My comment from 12 days ago is at https://news.ycombinator.com/item?id=21930569 https://news.ycombinator.com/item?i
by eesmith 7y ago
Fourth time this has been posted in 12 days. My comment from 12 days ago is at https://news.ycombinator.com/item?id=21930569 https://news.ycombinator.com/item?id=21930569 . I pointed out that kernprof profiling shows that 99+% of the time is spent in
s += num / fact
so none of the techniques describe give blinding speedup. I also suggest pre-compiling the regex.
- zeckalpha 7y agoThe lrucache recommendation could help if this was turned into a recursive / dynamically programmed approach.
- tcbasche 7y agoPre-compiling regex doesn't actually give you any real performance benefit as Python3 caches it internally anyway. See https://docs.python.org/3/library/re.html#re.compile https://docs.python.org/3/library/re.html#re.compile
- cpburns2009 7y agoThe cache is of a limited unknown size. It's more reliable to explicitly precompile your regular expressions. EDIT: The cache appears to be 512 on Python 3.6 so maybe precompiling isn't necessary unless you frequently use a large number of regular expressions.
- eesmith 7y agoSure. My original, linked-to comment ended: > 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. cpburns2009 says its 512 these days, which doesn't change the essence of my comment.