in reply to Re: Python regex faster than Perl?
in thread Python regex faster than Perl?

Thank you for your input, choroba. I've run each of the programs on input generated by your command and it seems that the Perl version does indeed best the Python one. My original input was an expanded form of the Day 3, Advent of Code 2024 input.

So I modified your snippet to run 10000000 iterations, which resulted in a 99MiB file for me. This is similar in size to the original input I used. In doing that, I found that the Perl version again runs slower: 0.626s, to Python's 0.416s.

Can you replicate this? It indicates to me that perhaps Python's regex implementation scales better.

-- edit: Or perhaps Python's interpreter has a larger startup cost and its regex implementation is indeed faster in this case...

Replies are listed 'Best First'.
Re^3: Python regex faster than Perl?
by choroba (Cardinal) on Mar 26, 2025 at 16:34 UTC
    I can confirm the behaviour. On my machine, it's 0.582s for Perl and 0.403s for Python.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re^3: Python regex faster than Perl?
by bliako (Abbot) on Mar 26, 2025 at 19:29 UTC

    I can also confirm perl 0.612s vs python 0.406s

    I have measured the time reading the file only. perl is twice as fast as python:

    Perl: real 0m0.064s user 0m0.016s sys 0m0.047s Python: real 0m0.123s user 0m0.046s sys 0m0.075s
Re^3: Python regex faster than Perl?
by LanX (Saint) on Mar 26, 2025 at 18:41 UTC
    I find speed differences of 20% neglectable.

    I seem to remember¹ that Perl's regex engine got slower because of all the extra features added.

    Real "Regexes" should be implemented as a high performance state machine, but perl is using op-codes now to cover those super features.

    YMMV if it's worth the slow down...

    Keep in mind that Ruby is/was half as fast as Perl and not many cared because of the observed "benefits".

    In a world of scalable cloud computing, speed isn't anymore the decisive factor it used to be.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

    ¹) I'm using this phrase often lately, but I posted too much here, and got tired double checking everything which was already written dozens of times.

    So a cloud of doubt might motivate others to add detailed information. Otherwise there is no interest, and it was the right decision not to invest more time.