I'm unsure why/how the python example was so slow in this testing. When I verbatim tested these scripts (and your sample a.txt file), I got that bash was completing in 0.27 s and that Python3 (version 3.8.9) was completing in 1.71 seconds. I was no where near the 9+ seconds specified in your example, and I tested this on multiple machines.
If I pre-compiled the Python regex pattern (pattern = re.compile(r'y')), then the performance dramatically improves to 0.5 seconds.
2 other interesting cases (which might be more applicable to Perl/Python comparison):
The Python was eventually faster than the Perl script with larger files and pattern matches (a file of 1,000,000 lines and 1,000 characters per line)
Perl showed significant performance differences based on how the regex pattern was defined. A hardcoded regex pattern ($line =~ /y/) was slightly faster than using a pattern stored as a scalar variable ($line =~ /\Q$pattern\E/). These were both way faster than getting the pattern from an array. I created a single-element array with the element being 'y', the script took over TWICE as long to complete as with the scalar variable.
https://code-maven.com/compare-the-speed-of-grep-with-python-regex
I'm unsure why/how the python example was so slow in this testing. When I verbatim tested these scripts (and your sample a.txt file), I got that bash was completing in 0.27 s and that Python3 (version 3.8.9) was completing in 1.71 seconds. I was no where near the 9+ seconds specified in your example, and I tested this on multiple machines.
If I pre-compiled the Python regex pattern (
pattern = re.compile(r'y')
), then the performance dramatically improves to 0.5 seconds.2 other interesting cases (which might be more applicable to Perl/Python comparison):
$line =~ /y/
) was slightly faster than using a pattern stored as a scalar variable ($line =~ /\Q$pattern\E/
). These were both way faster than getting the pattern from an array. I created a single-element array with the element being 'y', the script took over TWICE as long to complete as with the scalar variable.