# 10大python加速技巧

## 简介

import numpy as np


## 1 List Comprehension

• 在循环的每次迭代中我们都需要调用append，然后在循环的每次迭代中将其作为函数调用。
%%time
numbers = []
for x in range(10000000):
if x % 2 == 0:
numbers.append(x**2)


CPU times: user 2.33 s, sys: 81.6 ms, total: 2.41 s

Wall time: 2.43 s

%%time
numbers = [x**2 for x in range(10000000) if x % 2 == 0]


CPU times: user 1.89 s, sys: 93.8 ms, total: 1.99 s

Wall time: 2 s

## 2 使用built-in函数

python中非常多自带的函数采用了较多的加速，有些是使用C进行了加速。所以会比我们自己写一些for函数等快很多

%%time
def builtin_sum():
return sum(range(100000000))
_ = builtin_sum()


*CPU times: user 1.74 s, sys: 18.8 ms, total: 1.75 s*

*Wall time: 1.78 s*

%%time
def loop_sum():
s = 0
for i in range(100000000):
s += 1
return s
_ = loop_sum()


*CPU times: user 5.44 s, sys: 24.9 ms, total: 5.47 s*

*Wall time: 5.51 s*

## 3 尽可能不调用函数

%%time
def square(num):
return num**2

squares = []
for i in range(1000000):
squares.append(square(i))



CPU times: user 421 ms, sys: 23.7 ms, total: 445 ms

Wall time: 452 ms*

%%time
def squares():
squares = []
for i in range(1000000):
squares.append(i**2)
return squares
_ = squares()


CPU times: user 329 ms, sys: 19.5 ms, total: 348 ms

Wall time: 358 ms

## 4 尽可能使用numpy对数据进行加速

%%time
python_list = [i for i in range(1000000)]

_ = [i**2 for i in python_list]


CPU times: user 333 ms, sys: 42.7 ms, total: 376 ms

Wall time: 383 ms

%%time
numpy_array = np.array([i for i in range(1000000)])
_ = np.square(numpy_array)


CPU times: user 124 ms, sys: 29.7 ms, total: 153 ms

Wall time: 155 ms

## 5 numpy >= built-in

%%time
def numpy_sum():
return np.sum(np.arange(0,10000000))

_ =numpy_sum()


CPU times: user 27.1 ms, sys: 10.7 ms, total: 37.8 ms

Wall time: 37.1 ms

%%time
def builtin_sum():
return sum(range(10000000))
_ = builtin_sum()


CPU times: user 169 ms, sys: 1.17 ms, total: 170 ms

Wall time: 170 ms

## 6 避免Global Variables

Python中的全局变量不是最好的选择。

• 通常使用局部变量能更好地跟踪位置和内存使用情况。除了内存使用之外，Python在检索局部变量方面也比全局变量略快。

## 7 处理字符串尽可能使用字符串自带的函数

from collections import Counter
sequence = "AGAGKTAGAT" * 10000000

%%time
def count_string(seq):
return [seq.count("A"), seq.count('G'), seq.count('T'), seq.count('K')]

_ = count_string(sequence)


CPU times: user 293 ms, sys: 2.73 ms, total: 296 ms

Wall time: 296 ms

%%time
def count_Counter(seq):
counter = Counter(seq)
return [counter["A"], counter["G"], counter["T"], counter["K"]]
_ = count_Counter(sequence)


CPU times: user 4.25 s, sys: 30.1 ms, total: 4.28 s

Wall time: 4.36 s

## 8 使用多个变量一起赋值

%%time
a = 2
b = 3
c = 5
d = 7


CPU times: user 3 µs, sys: 1e+03 ns, total: 4 µs

Wall time: 6.91 µs

%%time
a, b, c, d = 2, 3, 5, 7


CPU times: user 3 µs, sys: 1e+03 ns, total: 4 µs

Wall time: 5.25 µs

## 9 while 1取代while True

%%time
cnt  = 0
while 1:
cnt  += 1
if cnt >= 100000:
break


CPU times: user 11.1 ms, sys: 699 µs, total: 11.8 ms

Wall time: 12.6 ms

%%time
cnt  = 0
while True:
cnt  += 1
if cnt >= 100000:
break


CPU times: user 12.8 ms, sys: 365 µs, total: 13.1 ms

Wall time: 14.2 ms

## 10 使用最新的python工具包

posted @ 2022-08-14 12:09  孤飞  阅读(1867)  评论(0编辑  收藏  举报