不同读入方式read(),readline(),readlines() 耗时对比

编写一个模块,该模块实现的功能如下:
1、引入grammar模块,通过引入的grammar模块生成句子,并把生成的句子写到名字为data.txt的文件中,写1亿次。
2、参考课上讲解的代码,通过装饰器测试在几种不同读取数据(读取自己生成的data.txt文件)的方式下的运行时间。

''' 伪代码
Step1. 编写主函数导入模块生成目标文件data.txt并调用read(),readline()和readlines()
三个函数读取目标文件data.txt,接收函数返回的消耗时间并输出
with open('data,txt', 'w') as file_object:
    for i in range(100000000):
        file_object.write(' '.join(sentence()) + '\n')
    read(file)
    readline(file)
    readlines(file)
step2. 编写三个辅助函数read(),readline()和readlines(),仅以readline举例
def readline(file)
    with open(file,'r') as f:
        while True:
            line = f.readline()
            if line == '':
                break
Step3. 编辑装饰器timer(),外层函数计时,并返回调用inner()函数;内层inner提示
用户进度并实现对read(),readline()和readlines()三个辅助函数的调用,并分别返
回消耗时间
def timer(read):
    def inner(x):
        start = time.time()
        read(x)
        end = time.time()
        return end - start
    return inner
Step4. 辅助函数前分别加装time@装饰器
@timer
def read(file): ...def readline(file):...def readlines(file):
Step5. 其他优化,如时间格式化输出等
'''

from grammar import sentence
import time
#装饰器函数
def timer(read):
    def inner(x):
        print('-RUNNING-')
        start = time.time()
        read(x)
        end = time.time()
        record = end - start
        return record
    return inner

@timer
def read(file):
    with open(file, 'r') as file_object:
        file_object.read()

@timer
def readline(file):
    with open(file, 'r') as file_object:
        while True:
            line = file_object.readline()
            if len(line) == 0:
                break

@timer
def readlines(file):
    with open(file, 'r') as file_object:
        file_object.readlines()
#主函数
if __name__ == '__main__':
    file = 'data.txt'
    with open(file, 'w+') as file_object:
        for i in range(10_000_000):
            file_object.write(' '.join(sentence()) + '\n')
    t = read(file)
    print("In 'read()' way, {:.2f} seconds  have pasted.".format(t))
    t = readline(file)
    print("In 'readline()' way, {:.2f} seconds  have pasted.".format(t))
    t = readlines(file)
    print("In 'readlines()' way, {:.2f} seconds  have pasted.".format(t))

导入的grammar模块

from random import choice, seed, randrange

articles = ("a", "the","1","2","3","4","5","6","7","8","9","10")
nouns = ("cat", "dog", "sheep", "rabbit", "tiger", "chicken",
         "fish", "grass", "seed", "carrot", "apple")
verbs = ("eats", "catches", "finds")

def sentence():
    return noun_phrase() + verb_phrase()

def noun_phrase():
    return [choice(articles), choice(nouns)]

def verb_phrase():
    vp = [choice(verbs)]
    if randrange(3) > 0:
        vp.extend(noun_phrase())
    return vp
    
if __name__ == "__main__":
    seed()
    for i in range(10):
        print(" ".join(sentence()))

运行结果

-RUNNING-
In 'read()' way, 7.41 seconds  have pasted.
-RUNNING-
In 'readline()' way, 16.03 seconds  have pasted.
-RUNNING-
In 'readlines()' way, 21.48 seconds  have pasted.

你可能感兴趣的:(菜鸟学习)