python程序性能优化

最近工作中有个任务,就是优化一个模型的实时性。从有到无,主要完成了以下内容。

0.模型的逻辑

1.算法逻辑

2.代码重构

3.程序的性能优化,包括编译、多线程、多进程、numba

4.语言

numba包,经测试,比较适用于数组、矩阵等数值计算,其他的类型操作,容易报错。

from multiprocessing import Pool
from functools import  partial

def math_get(a, b):
    print(a)
    return b + a
def processing(b):
    pool = Pool(3)
    a = [i for i in range(3)]
    d = pool.map(partial(math_get,b = b), a,)
#多个参数代入的时候,采用partial的方式,
#其中a作为一个可迭代的对象通过map代入math_get函数
    print(d)
    pool.close()
    pool.join()
if __name__ == "__main__":
    processing(5)
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'Great'
from multiprocessing import Pool
import time
def a(num):
    list_num = []
    print(num)
    for i in num:
        list_num.append(i)
        print(i)
        print(list_num)
    return list_num

if __name__ == "__main__":

    start = time.time()
    pool = Pool(3)#进程池
    num = [1,2,3,4,5,6]
   #pool.apply_async(a,args=(num,))
    a = pool.map(a,(num, ))
    print('a',a)
    pool.close()
    pool.join()
    end = time.time()
    print(end-start)
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'Great'
from multiprocessing import Pool
import time
from functools import  partial
def a(num, ccc):
    list_num = []

    for i in num:
        #list_num.append(i)
        print(i + ccc)
        #print(list_num)
    #return list_num

if __name__ == "__main__":

    start = time.time()
    pool = Pool(3)

    num = [1,2,3,4,5,6]
    ccc = 1
   #for i in range(10):
   #pool.apply_async(a,args=(num,))
    pool.map(partial(a,ccc = ccc),(num,))#多个参数
    #print('a',a)
    pool.close()
    pool.join()
    end = time.time()
    print(end-start)

猜你喜欢

转载自blog.csdn.net/weixin_41512727/article/details/82772889