Get the App
SLTechnology News&Howtos  ›  Servers  › 

An example Analysis of the comparison of text search efficiency between Python and sed,grep

Shulou Source: shulou.com Published: 2022-06-01 04:54:49 10月07日 Update

Editor to share with you Python and sed,grep text search efficiency comparison of the example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

Test text: 20w lines, 21m large

Python normal regular matching:

#! / usr/bin/python3

Import re

F=open ('/ tmp/test.txt')

For line in f:

Match=re.findall ('^ This.*want',line)

If match! = []:

Print (match)

Results:

Try the compiled regular:

The code is as follows:

#! / usr/bin/python3

Import re

F=open ('/ tmp/test.txt')

Re_obj=re.compile ('^ This.*want')

For line in f:

Match=re_obj.findall (line)

If match! = []:

Print (match)

The result is twice as fast:

Try sed:

It's an order of magnitude faster!

Finally, try grep:

Sure enough, grep is looking for the most professional!

The above is all the contents of the article "example Analysis of the comparison of Python and sed,grep text search efficiency". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Text articles efficiency examples analysis content regularity results ordinary lower editor not so much professional number code most more knowledge industry information information channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker OPPO Reno Redmi MySQL vpn