Three ways Python reads the contents of a file and compares

Source: Internet
Author: User

The file for this experiment is a 60M file totaling 392660 lines of content.

Program One:

defOne (): Start=time.clock () fo= Open (file,'R') FC=fo.readlines () Num=0 forLinchFc:tup= L.rstrip ('\ n'). Rstrip (). Split ('\ t') Num= Num+1fo.close () End=Time.clock ()Printend-StartPrintNum

Running Result: 0.812143868027s

Program Two:

defBoth (): Start=time.clock () Num=0 with open (file,'R') as F: forLinchF:tup= L.rstrip ('\ n'). Rstrip (). Split ('\ t') Num= Num+1End=time.clock () times= (end-start)Print TimesPrintNum

Running time: 0.74222778078

Program Three:

defthree (): Start=time.clock () fo= Open (file,'R') L=fo.readline () Num=0 whileL:tup= L.rstrip ('\ n'). Rstrip (). Split ('\ t') L=fo.readline () Num= Num+1End=Time.clock ()Printend-StartPrintNum

Running time: 1.02316120797

The result can be drawn, program two speed is the fastest.

Three ways Python reads the contents of a file and compares

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.