逐行分析Python的耗时,提高GPU利用率30%

狂风中的少年 提交于 2020-02-25 19:22:08

背景:
因为训练模型时,GPU利用率一直上不去,在1% -5%,通过下面几行代码,分析出
gc.collect()
耗时占了67%,注释掉GPU利用率,涨到了30%,感谢这个文章,特此转载

原文:https://blog.csdn.net/rookie_is_me/article/details/88866010

import line_profiler
 
def prime_num(max_num):
    for num in range(2, max_num):
        if num < 2:
            pass
        elif num == 2:
            pass
        else:
            for i in range(2, num):
                if num % i == 0:
                    break
            else:
                pass


#这将要运行的函数,放在中间
profile = line_profiler.LineProfiler(prime_num)  # 把函数传递到性能分析器
profile.enable()  # 开始分析
prime_num(3000) # 要执行的函数放在这里,代码执行完自会打印耗时的信息
profile.disable()  # 停止分析
profile.print_stats()  # 打印出性能分析结果
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!