zoukankan      html  css  js  c++  java
  • python变量内存地址释放与加速并行计算多线程

    1、导入numba和gc包进行并行计算和内存释放

      代码如下很容易的:

    #coding:utf-8
    import time
    
    from numba import jit, prange, vectorize
    from numba import cuda
    from numba import njit
    import numpy as np
    import gc
    
    
    def adds(x,y,m):
        return [x*i for i in range(y)]
    
    @jit(parallel=True,nogil=True)
    # @njit(parallel=True,nogil=True)
    def adds1(x,y,m):
        sd =  np.empty((y))
        for i in prange(y):
            for j in range(m):
                sd[i]=x*i*m
        return sd
    
    @jit(parallel=True,nogil=True)
    def test(n):
        temp = np.empty((50, 50)) # <--- allocation is hoisted as a loop invariant as `np.empty` is considered pure
        for i in prange(n):
            temp[:] = 0           # <--- this remains as assignment is a side effect
            for j in range(50):
                temp[j, j] = i
        return temp
    
    if __name__=="__main__":
        n = 50
        max = 10000**2*12
        m=100
        # st1 = time.time()
        # val_1 = adds(n,max,m)
        # print(time.time()-st1)
    
        st2 = time.time()
        val_2 = adds1(n,max,m)
        print(time.time()-st2)
        # 释放内存地址
        del val_2,n,max,m
        gc.collect()
    
        st3 = time.time()
        tmp = test(100**3*10)
        print(time.time()-st3)
        # 释放temp的内存地址
        del tmp
        gc.collect()
    

      

  • 相关阅读:
    Pandas的高级操作
    Pandas的拼接操作
    Matplotlib基础使用
    股票分析案例
    Pandas处理缺失的数据
    Pandas的基础使用
    python前端之CSS基础--常用样式
    python前端之CSS介绍--选择器
    python实现网站用户名密码自动登录
    Python前端HTML介绍
  • 原文地址:https://www.cnblogs.com/wuzaipei/p/10660663.html
Copyright © 2011-2022 走看看