测试效率 timeit cProfile

 

timeit使用

def f1(lIn):
    l1 = sorted(lIn)  # O(nlogn)  C语言的
    l2 = [i for i in l1 if i<0.5]  # O(n)
    return [i*i for i in l2]  # O(k)

def f2(lIn):
    l1 = [i for i in lIn if i<0.5]  # O(n)
    l2 = sorted(l1) # O(klogk)  C语言的
    return [i*i for i in l2]  # O(k)

def f3(lIn):
    l1 = [i*i for i in lIn]  # O(n)
    l2 = sorted(l1)  # O(nlogn)  C语言的
    return [i for i in l2 if i<(0.5*0.5)]  # O(n)


import timeit
import random
l = [random.random() for i in range(100000)]

t1 = timeit.Timer("f1(l)","from __main__ import random,l,f1")
t2 = timeit.Timer("f2(l)","from __main__ import random,l,f2")
t3 = timeit.Timer("f3(l)","from __main__ import random,l,f3")
print(t1.timeit(number=50))  # 3.0630057093816947
print(t2.timeit(number=50))  # 1.6970076176407773
print(t3.timeit(number=50))  # 3.30195772223185

或使用Ipython

def f1(lIn):
    l1 = sorted(lIn)  # O(nlogn)  C语言的
    l2 = [i for i in l1 if i<0.5]  # O(n)
    return [i*i for i in l2]  # O(k)
def f2(lIn):
    l1 = [i for i in lIn if i<0.5]  # O(n)
    l2 = sorted(l1) # O(klogk)  C语言的
    return [i*i for i in l2]  # O(k)
def f3(lIn):
    l1 = [i*i for i in lIn]  # O(n)
    l2 = sorted(l1)  # O(nlogn)  C语言的
    return [i for i in l2 if i<(0.5*0.5)]  # O(n)
import random
lIn = [random.random() for i in range(100000)]


%timeit f1(lIn)
75.8 ms ± 917 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
%timeit f2(lIn)
39.3 ms ± 560 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
%timeit f3(lIn)
80 ms ± 1.09 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

 

 

 

使用cProfile

def f1(lIn):
    l1 = sorted(lIn)  # O(nlogn)  C语言的
    l2 = [i for i in l1 if i<0.5]  # O(n)
    return [i*i for i in l2]  # O(k)

def f2(lIn):
    l1 = [i for i in lIn if i<0.5]  # O(n)
    l2 = sorted(l1) # O(klogk)  C语言的
    return [i*i for i in l2]  # O(k)

def f3(lIn):
    l1 = [i*i for i in lIn]  # O(n)
    l2 = sorted(l1)  # O(nlogn)  C语言的
    return [i for i in l2 if i<(0.5*0.5)]  # O(n)


import cProfile
import random
lIn = [random.random() for i in range(100000)]
cProfile.run('f1(lIn)')
cProfile.run('f2(lIn)')
cProfile.run('f3(lIn)')

"""
         7 function calls in 0.125 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.120    0.120 11.py:1(f1)
        1    0.020    0.020    0.020    0.020 11.py:3(<listcomp>)
        1    0.009    0.009    0.009    0.009 11.py:4(<listcomp>)
        1    0.005    0.005    0.125    0.125 <string>:1(<module>)
        1    0.000    0.000    0.125    0.125 {built-in method builtins.exec}
        1    0.091    0.091    0.091    0.091 {built-in method builtins.sorted}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}


         7 function calls in 0.044 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.041    0.041 11.py:6(f2)
        1    0.007    0.007    0.007    0.007 11.py:7(<listcomp>)
        1    0.006    0.006    0.006    0.006 11.py:9(<listcomp>)
        1    0.003    0.003    0.044    0.044 <string>:1(<module>)
        1    0.000    0.000    0.044    0.044 {built-in method builtins.exec}
        1    0.028    0.028    0.028    0.028 {built-in method builtins.sorted}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}


         7 function calls in 0.068 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.065    0.065 11.py:11(f3)
        1    0.006    0.006    0.006    0.006 11.py:12(<listcomp>)
        1    0.010    0.010    0.010    0.010 11.py:14(<listcomp>)
        1    0.002    0.002    0.068    0.068 <string>:1(<module>)
        1    0.000    0.000    0.068    0.068 {built-in method builtins.exec}
        1    0.049    0.049    0.049    0.049 {built-in method builtins.sorted}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
"""

 

posted @ 2019-01-03 17:20  fat39  阅读(179)  评论(0编辑  收藏  举报