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