szabgab/perlmaven.com

compare-the-speed-of-perl-and-python-regex

Opened this issue · 0 comments

https://perlmaven.com/compare-the-speed-of-perl-and-python-regex

Oof. I know this is a pretty old post, but also very misleading. It says 20 times speed difference between Python and Perl, but it just uses regexp in Python in very inefficient way, and what is measures is actually not regexp speed, but other aspects of the language.

First: If you use c = re.compile(r'y') before you even a start of the loop, and then c.search(line), difference shrinks to 2 times. (Perl does it implicitly to some extent because it compiles regular expression at perl source code parsing time, something that JavaScript for example often does, but cannot be done with Python out of the box).

Second: If you actually first do a proper control test, and test just looping with a function does only return false, instead of calling regexp engine at all, you will see that the time difference as well as absolute different is still similar. Which means you are not testing regexp speed, but other things in the interpreter and language.

The article conclusion:

The regex engine in Perl is much faster than the regex engine of Python.

is simply false. It is faster, and easier to use, but not astronomically faster, and highly depends on regexp.

Sure, perl has easier to use and usually faster regexp, but the tests in the article are just done in a very lazy way. It took me more time to write this "comment" on github, than run proper tests.