检测函数运行时间

import string
import random
import timeit
from timeit import Timer


def random_string(length=16):
    rule = string.ascii_letters + string.digits
    char_list = random.sample(rule, length)
    return ''.join(char_list)

def random2():
    char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
    res = ''
    for i in range(16):
        res += random.choice(char)
    return res

t1=Timer("random_string()","from __main__ import random_string").timeit(100000)
t2=Timer("random2()","from __main__ import random2").timeit(100000)
print(t1)
print(t2)
posted @ 2020-01-06 11:10  JaryTom  阅读(192)  评论(0编辑  收藏  举报