Skip to content

Instantly share code, notes, and snippets.

@hrldcpr
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrldcpr/eb0dc31f6c98fed2b47f to your computer and use it in GitHub Desktop.
Save hrldcpr/eb0dc31f6c98fed2b47f to your computer and use it in GitHub Desktop.
comparison of lazy and greedy regex performance

greedy - <img [^>]*>

python3 -m timeit -s 'import re; r = re.compile(r"<img [^>]*>")' \
'r.findall("<img src alt name etc> <img even more stuff forever dot com forward slash junk.html and stuff>")'

1000000 loops, best of 3: 0.72 usec per loop

lazy - <img .*?>

python3 -m timeit -s 'import re; r = re.compile(r"<img .*?>")' \
'r.findall("<img src alt name etc> <img even more stuff forever dot com forward slash junk.html and stuff>")'

1000000 loops, best of 3: 1.77 usec per loop

/see http://blog.stevenlevithan.com/archives/greedy-lazy-performance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment