设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 手机 数据
当前位置: 首页 > 综合聚焦 > 编程要点 > 正文

python运行加速有什么方法?效果怎样?

发布时间:2022-01-14 14:21 所属栏目:13 来源:互联网
导读:python运行加速有什么方法?对于Python运行的慢问题这里就不多说,下面主要给大家来分享一下python运行加速的几种方式,有需要的朋友可以参考,接下来我们一起看看吧。 一、总结 1、使用pypy 2、减少函数化调用 3、减少文件的打开即with的调用,将这一调用放
       python运行加速有什么方法?对于Python运行的慢问题这里就不多说,下面主要给大家来分享一下python运行加速的几种方式,有需要的朋友可以参考,接下来我们一起看看吧。
 
    一、总结
    1、使用pypy
    2、减少函数化调用
    3、减少文件的打开即with的调用,将这一调用放在for循环前面,然后传递至后面需要用到的地方
    4、if函数判断条件多的尽量在前面
    全面加速(pypy)
 
    二、全面加速(pypy)
    将python换为pypy,在纯python代码下,pypy的兼容性就不影响使用了,因为一些纯python的代码常常会用pypy进行一下加速
 
    测试代码,for循环10000000次
 
start = time.time()
for i in range(10000000):
    print(i,end="\r")
end = time.time()
print(f"耗费时间{end-start}秒>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
             
 
#后续在多个if-elif-else中调用
    耗时为
 
    tqdm预估时间在15~25个小时左右跳动
 
    将with放在循环前面
    如
 
    将with的内容作为f_传递进来
 
 
 
    后的耗时为:
 
 
    测试如下:
 
import os, warnings,time,tqdm
def txt(word):
    with open("ceshi.txt","a+",encoding="utf-8")as f:
        if len(str(word))<=2:
            word+=100
            f.write(str(word)+"\n")
        elif 2<len(str(word))<=4:
            word+=200
            f.write(str(word)+"\n")
        else:
            f.write(str(word) + "\n")
if __name__=="__main__":
    start = time.time()
    for i in tqdm.tqdm(range(100000)):
        txt(i)
    end = time.time()
    print(f"耗费时间{end-start}秒>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
    耗时结果为:
 
    将文件的打开即with的调用放在外面
 
import os, warnings,time,tqdm
def txt(f,word):
 
        if len(str(word))<=2:
            word+=100
            f.write(str(word)+"\n")
        elif 2<len(str(word))<=4:
            word+=200
            f.write(str(word)+"\n")
        else:
            f.write(str(word) + "\n")
if __name__=="__main__":
    start = time.time()
    with open("ceshi.txt", "a+", encoding="utf-8")as f:
        for i in tqdm.tqdm(range(100000)):
 
            txt(f,i)
    end = time.time()
    print(f"耗费时间{end-start}秒>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
    耗时为
 
    结论:快了119倍,而实际加速远远大于这个倍数
 
    三、if判断靠前
    如:
 
 if tag in ["nts", "nto", "ntc", "ntcb", "ntcf", "ntch", "nth", "ntu", "nt"]:
                                BMES(f_,i2, tag="ORG")
                            elif tag in ["nb", "nba", "nbc", "nbp", "nf", "nm", "nmc", "nhm", "nh"]:
                                BMES(f_,i2, tag="OBJ")
                            elif tag in ["nnd", "nnt", "nn"]:
                                BMES(f_,i2, tag="JOB")
                            elif tag in ["nr", "nrf"]:
                                BMES(f_,i2, tag="PER")
                            elif tag in ["t"]:
                                BMES(f_,i2, tag="TIME")
                            elif tag in ["ns", "nsf"]:
                                BMES(f_,i2, tag="LOC")
                            else:
                                for i3 in list(i2):
                                    f_.write(i3 + " " + f"O" + "\n")
    满足条件的可以先跳出判断

(编辑:ASP站长网)

    网友评论
    推荐文章
      热点阅读