24式加速你的Python

24式加速来自 (梁云1991 Python爱好者社区)文章,自己把程序敲了一遍,把测试结果上传到CSDN文章中
第1式,测算代码运行时间
import time
tic = time.time()
much_job = [x**2 for x in range(1,1000000,3)]
toc = time.time()
print('used {:.5}s'.format(toc - tic))
C:\Users\Administrator\Anaconda3\python.exe C:/TensorFlow-Coding/24式加速你的Python.py
used 0.12032s

第2式,测算代码多次运行平均时间

from timeit import timeit
g = lambda x:x**2+1
def main():
    return(g(2)**120)
print(timeit('main()', globals = {'main':main}, number = 10))
C:\Users\Administrator\Anaconda3\python.exe C:/TensorFlow-Coding/24式加速你的Python.py
1.3492889794087563e-05

第3式,按调用函数分析代码运行时间

def rule(x):
    return (x if x>0 else 0)
def main():
    result = [rule(x) for x in range(-100000, 100000,1)]
    return result
import profile
profile.run('main()')
C:\Users\Administrator\Anaconda3\python.exe C:/TensorFlow-Coding/24式加速你的Python.py
         200006 function calls in 0.266 seconds
   Ordered by: standard name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
   200000    0.156    0.000    0.156    0.000 24式加速你的Python.py:2(rule)
        1    0.000    0.000    0.266    0.266 24式加速你的Python.py:4(main)
        1    0.109    0.109    0.266    0.266 24式加速你的Python.py:5(<listcomp>)
        1    0.000    0.000    0.266    0.266 :0(exec)
        1    0.000    0.000    0.000    0.000 :0(setprofile)
        1    0.000    0.000    0.266    0.266 <string>:1(<module>)
        1    0.000    0.000    0.266    0.266 profile:0(main())
        0    0.000             0.000          profile:0(profiler)
Process finished with exit code 0

第4式,按行分析代码运行时间

#(base) C:\Users\Administrator>conda install line_profiler
def relu(x):
    return(x if x > 0 else 0)
def main():
    result = [relu(x) for x in range(-100000, 100000, 1)]
    return result
from line_profiler import LineProfiler
lprofile = LineProfiler(main, relu)
lprofile.run('main()')
lprofile.print_stats()

C:\Users\Administrator\Anaconda3\python.exe C:/TensorFlow-Coding/24式加速你的Python.py
Timer unit: 3.64673e-07 s

Total time: 0.0670254 s
File: C:/TensorFlow-Coding/24式加速你的Python.py
Function: relu at line 2

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     2                                           def relu(x):
     3    200000     183796.0      0.9    100.0      return(x if x > 0 else 0)

Total time: 0.233613 s
File: C:/TensorFlow-Coding/24式加速你的Python.py
Function: main at line 4

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     4                                           def main():
     5         1     640609.0 640609.0    100.0      result = [relu(x) for x in range(-100000, 100000, 1)]
     6         1          1.0      1.0      0.0      return result


Process finished with exit code 0

二,加速你的查找

第5式,用set而非list进行查找

#List列表查找
import time
data = (i**2 + 1 for i in range(1000000))
list_data = list(data)
tic=time.time()
list_data.index(856329995162)
toc=time.time()


print('used {:.5}s'.format(toc - tic))
C:\Users\Administrator\Anaconda3\python.exe C:/TensorFlow-Coding/24式加速你的Python.py
used 0.015005s
#set查找
import time
data = (i**2 + 1 for i in range(1000000))
set_data =  set(data)
tic=time.time()
856324442885 in set_data
toc=time.time()
print('used {:.5}s'.format(toc - tic))
C:\Users\Administrator\Anaconda3\python.exe C:/TensorFlow-Coding/24式加速你的Python.py
used 0.0s
#第6式,用dict而非两个list进行匹配查找
import time
list_a = [2*i-1 for i in range(1000000)]
list_b = [i**2 for i in list_a]
dict_ab = dict(zip(list_a,list_b))
tic=time.time()
print(list_b[list_a.index(876567)])
toc=time.time()
print('used {:.5}s'.format(toc - tic))

tic=time.time()
print(dict_ab.get(876567,None))
toc=time.time()
print('used {:.5}s'.format(toc - tic))
运算结果
used 0.0050125s
used 0.0s

三,加速你的循环

#第6式,用dict而非两个list进行匹配查找
import time

tic=time.time()
s, i = 0,0
while i<100000000:
    i=i+1
    s=s+i
toc=time.time()
print('used {:.5}s'.format(toc - tic))

tic=time.time()
s=0
for i in range(1, 100000000):
    s = s+i
toc=time.time()
print('used {:.5}s'.format(toc - tic))

第8式,在循环体中避免重复计算

import time
a=[i**2 + 1 for i in range(2000)]
tic=time.time()
b=[i/sum(a) for i in a]
toc=time.time()
print('used {:.5}s'.format(toc - tic))

tic=time.time()
sum_a=sum(a)
b=[i/sum_a for i in a]
toc=time.time()
print('used {:.5}s'.format(toc - tic))

C:\Users\Administrator\Anaconda3\python.exe C:/TensorFlow-Coding/24式加速你的Python.py
used 0.035089s
used 0.0s
运算结果
C:\Users\Administrator\Anaconda3\python.exe C:/TensorFlow-Coding/24式加速你的Python.py
used 14.73s
used 9.3419s

四,加速你的函数

第9式,用循环机制代替递归函数

 

import time

tic=time.time()
def fib(n):
    return(1 if n in (1,2) else fib(n-1)+fib(n-2))
fib(40)
toc=time.time()
print('used {:.5}s'.format(toc - tic))

tic=time.time()
def fib(n):
    if n in(1,2):
        return (1)
    a,b = 1,1
    for i in range(2,n):
        a,b = b, a+b
    return (b)
fib(40)
toc=time.time()
print('used {:.5}s'.format(toc - tic))
C:\Users\Administrator\Anaconda3\python.exe C:/TensorFlow-Coding/24式加速你的Python.py
used 24.615s
used 0.0s

第10式,用缓存机制加速递归函数

import time
from functools import lru_cache
tic=time.time()
def fib(n):
    return(1 if n in (1,2) else fib(n-1)+fib(n-2))
fib(40)
toc=time.time()
print('used {:.5}s'.format(toc - tic))

tic=time.time()
@lru_cache(100)
def fib(n):
    return (1 if n in (1,2) else fib(n-1)+fib(n-2))
fib(40)
toc=time.time()
print('used {:.5}s'.format(toc - tic))

C:\Users\Administrator\Anaconda3\python.exe C:/TensorFlow-Coding/24式加速你的Python.py
used 24.592s
used 0.0s
Process finished with exit code 0

 

第11式,用numba加速Python函数

import time
from numba import jit
tic=time.time()
def my_power(x):
    return (x**2)
def my_power_sum(n):
    s = 0
    for i in range(1, n+1):
        s = s + my_power(i)
    return (s)
my_power_sum(1000000)
toc=time.time()
print('used {:.5}s'.format(toc - tic))

tic=time.time()
@jit
def my_power(x):
    return (x**2)
@jit
def my_power_sum(n):
    s = 0
    for i in range(1, n+1):
        s=s + my_power(i)
    return (s)
my_power_sum(1000000)
toc=time.time()
print('used {:.5}s'.format(toc - tic))
C:\Users\Administrator\Anaconda3\python.exe C:/TensorFlow-Coding/24式加速你的Python.py
used 0.42312s
used 0.16746s

五,使用标准库函数进行加速

第12式,使用collections.Counter加速计数

 

import time
data = [x**2%1989 for x in range(2000000)]
tic=time.time()
values_count = {}
for i in data:
    i_cnt = values_count.get(i, 0)
    values_count[i] = i_cnt + 1
values_count.get(4,0)
toc=time.time()
print('used {:.5}s'.format(toc - tic))

tic=time.time()
from collections import Counter
values_count = Counter(data)
values_count.get(4,0)
toc=time.time()
print('used {:.5}s'.format(toc - tic))
C:\Users\Administrator\Anaconda3\python.exe C:/TensorFlow-Coding/24式加速你的Python.py
used 0.54398s
used 0.15843s

Process finished with exit code 0

第13式,使用collections.ChainMap加速字典合并

 

import time
from collections import ChainMap
dic_a = {i:i+1 for i in range(1, 1000000,2)}
dic_b = {i:2*i+1 for i in range(1, 1000000,3)}
dic_c = {i:3*i+1 for i in range(1,1000000,5)}
dic_d = {i:4*i+1 for i in range(1,1000000,7)}

tic=time.time()
result=dic_a.copy()
result.update(dic_b)
result.update(dic_c)
result.update(dic_d)
result.get(9999,0)
toc=time.time()
print('used {:.5}s'.format(toc - tic))

tic=time.time()
chain = ChainMap(dic_a, dic_b, dic_c,dic_d)
chain.get(9999,0)
toc=time.time()
print('used {:.5}s'.format(toc - tic))

C:\Users\Administrator\Anaconda3\python.exe C:/TensorFlow-Coding/24式加速你的Python.py
used 0.071187s
used 0.0s

六,使用高阶函数进行加速

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值