十大Python加速技巧,首先导入numpy
import numpy as np
将for的append操作替换为列表中直接产出。这其中加速的主要原因是:
1
2
3
4
5
%%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
1
2
%%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
python中非常多自带的函数采用了较多的加速,有些是使用C进行了加速。所以会比我们自己写一些for函数等快很多
1
2
3
4
%%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*
1
2
3
4
5
6
7
%%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*
在所有的函数语言中,对于函数的调用都是相对更加耗时的,所以在能不适用函数调用的时候尽可能不调用函数,虽然这会使我们的代码更佳简洁易读。
1
2
3
4
5
6
7
8
%%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*
1
2
3
4
5
6
7
%%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
因为numpy是使用C语言进行过加速的,所以相对于其它很多数据操作是更加快速的。
1
2
3
4
%%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
1
2
3
%%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
1
2
3
4
5
%%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
1
2
3
4
%%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
Python中的全局变量不是最好的选择。
因此,在可能的情况下,最好避免使用全局变量。
在处理字符串的时候尽可能使用字符串自带的函数,往往是针对性的优化过,会比我们调用一些其它的工具包来处理特定的数据类型要快很多。
1
2
from collections import Counter
sequence = "AGAGKTAGAT" * 10000000
1
2
3
4
5
%%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
1
2
3
4
5
%%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
1
2
3
4
5
%%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
1
2
%%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
1
2
3
4
5
6
%%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
1
2
3
4
5
6
%%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
一般后续的新的工具包往往比过往的python工具包要快很多,所以能更新到新的板块则可以尽快更新。
原创作者:孤飞-博客园
原文链接:https://www.cnblogs.com/ranxi169/p/16585192.html
未经允许不得转载:肥猫博客 » 10大python加速技巧