for循环用了多种方式,做大数据计算的时候,实际用的时间都差不多,想提速,但不知道怎么办求吾爱大神解惑,这种情况是我硬件配置问题?还是代码问题?运行时,CPU只占15%,内存也没超过一半,系统win10_64位,编译器pycharm
代码思路1:普通for循环
[Python] 纯文本查看 复制代码 def data_function1(): # 数据计算方式1-for循环
start = time.perf_counter_ns()
for i in ret:
# print(i)
if i == 5:
print(i)
end = time.perf_counter_ns()
print('for循环耗时:{}'.format(str(round(end - start, 2))))
data_function1()
代码思路2:多线程代替for循环
[Python] 纯文本查看 复制代码 import time
from multiprocessing.dummy import Pool
ret = list(range(1, 6)) # 数据2
def process(x): # 代替for循环,数据计算方式2函数主体
# print(x)
if x == 5:
print(x)
def data_function2(): # 计算方式2-多线程代替for循环
start = time.perf_counter_ns()
pool = Pool()
pool.map(process, ret)
pool.close()
pool.join()
end = time.perf_counter_ns()
print('多线程耗时:{}'.format(str(round(end - start, 2))))
用@na.jit()方式,实际效果也不明显,还报错多
[Python] 纯文本查看 复制代码 import time
from multiprocessing.dummy import Pool
import numba as na
ret = list(range(1, 6)) # 数据2
@na.jit() # 自动进行并行计算(nopython=True, parallel=True)
def data_function3(): # 数据计算方式3-for循环+jit
start = time.perf_counter()
for i in ret:
# print(i)
if i == 5:
print(i)
end = time.perf_counter()
print('for循环jit耗时:{}'.format(str(round(end - start, 2))))
最后用一个笨办法
[Python] 纯文本查看 复制代码 ret = list(range(1, 6))
A = [1, 2, 3]
B = [4, 5, 6]
def data_A(): # 数据计算方式1-for循环
start = time.perf_counter_ns()
for i in A:
# print(i)
if i == 5:
print(i)
end = time.perf_counter_ns()
print('for循环耗时:{}'.format(str(round(end - start, 2))))
def data_B(): # 数据计算方式1-for循环
start = time.perf_counter_ns()
for i in B:
# print(i)
if i == 5:
print(i)
end = time.perf_counter_ns()
print('for循环耗时:{}'.format(str(round(end - start, 2))))
# 将原列表分成两部分,分别代入两个函数,用threading方法同时启动两个函数
效果还是一样的,几种方式运行的时间都差不多,求万能的吾爱大神指点,谢谢!
|