lbk60ox 发表于 2024-8-17 20:59:10

Python运行速度慢?是你孤陋寡闻了?5个优化办法提速百分百


    <h1 style="color: black; text-align: left; margin-bottom: 10px;">Python运行速度慢?是你孤陋寡闻了?5个优化<span style="color: black;">方法</span>提速百分百</h1>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/211b09d4aa9941fb800678d994b94c92~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723895544&amp;x-signature=9vaWRLNoQq%2BtOxOv3rO2zeS%2Fswk%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">北漂编程<span style="color: black;">2021-04-23 21:47</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">速度慢</h1>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/98befa9ba5b44cd0a92d317b2166f1da~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723895544&amp;x-signature=ajfrY5UHVpibiRZgFupdKiUvBCg%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python之<span style="color: black;">因此</span>速度慢,<span style="color: black;">重点</span>是<span style="color: black;">因为</span>动态性和多功能性。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python 的运行过程是虚拟机读入 Python 代码(文本),词法分析,编译成虚拟机认识的 opcode,<span style="color: black;">而后</span>虚拟机解释 opcode 执行。但这<span style="color: black;">并不</span>是最<span style="color: black;">重点</span>的<span style="color: black;">原由</span>,Python import 之后会缓存编译后的 opcode,<span style="color: black;">因此</span>读入、词法分析和编译并<span style="color: black;">无</span>占用太多的时间。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">那样</span>真正的慢的是哪一步分呢?<span style="color: black;">便是</span>后面的虚拟机解释 opcode 执行的部分。前期的编译是将 Python 代码编译成解释器<span style="color: black;">能够</span>理解的中间代码,解释器再将中间代码翻译成 CPU <span style="color: black;">能够</span>理解的指令。相比于 AOT(提前编译型语言,<span style="color: black;">例如</span>C)直接编译成<span style="color: black;">设备</span>码,肯定是慢的。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/9b080a3335e849a3ba96d171b879e0e8~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723895544&amp;x-signature=LqNoh2VFeKg2ex%2F%2BPzJIS7chejM%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">下面是五种已有的<span style="color: black;">方法</span>,<span style="color: black;">帮忙</span>你<span style="color: black;">加强</span>Python的性能。</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">Cython</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">是Python的C语言扩展,cPython是一门单独的语言,专门用来写在Python里面的import用的扩展库。CPython跟Python语法基本一致,而CPython有专门的编译器。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p26-sign.toutiaoimg.com/pgc-image/8c1a48f328a4430893541c9635a6aca0~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723895544&amp;x-signature=iuivvC0KgOca3glbtvwhZVqyIXQ%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">据述</span>,Cython<span style="color: black;">供给</span>了<span style="color: black;">有些</span>特性来让代码更<span style="color: black;">有效</span>,<span style="color: black;">例如</span>变量类型化,这本质上是C<span style="color: black;">需求</span>的。<span style="color: black;">有些</span>科学计算的包,如scikit-learn依赖Cython的<span style="color: black;">有些</span>特性来保持操作简洁快速。</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">Pyston</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Pyston,由Dropbox<span style="color: black;">帮助</span>,<span style="color: black;">运用</span>LLVM编译器架构来加速Python,<span style="color: black;">一样</span>的它<span style="color: black;">亦</span><span style="color: black;">运用</span>了适时编译。相比于PyPy,Pyston还<span style="color: black;">处在</span><span style="color: black;">初期</span><span style="color: black;">周期</span>,它只支持Python的部分特性。Pyston把工作分成两个部分,一部分是语言的核心特性,另一部分是把性能<span style="color: black;">提高</span>到可接受的程度。Pyston距离<span style="color: black;">能够</span>在生产环境<span style="color: black;">运用</span>还有一段距离</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/352ef67ce55e468c9281c6009065d3bb~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723895544&amp;x-signature=FqHPDBgXYeqICQmUXxOOFPYe0gA%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">Nuitka</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">是一个Python的替代品,它<span style="color: black;">能够</span>将Python代码转换为C++代码,<span style="color: black;">而后</span>编译为可执行文件,并且<span style="color: black;">经过</span>调用Python的API的方式实现从解析语言到编译语言的转换,在转换到C++的过程中直接<span style="color: black;">运用</span>python的解释器,<span style="color: black;">能够</span><span style="color: black;">保准</span>100%的语法兼容。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/b52806b8e1b24864abd0974f9c31576a~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723895544&amp;x-signature=LZIm06KkOv9Eaf0a00W5PEkJ23o%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">​</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">Numba</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Numba结合了上面几个项目的想法。学习了Cython,Numba<span style="color: black;">亦</span>采用了部分加速的策略,只加速CPU密集型的任务;<span style="color: black;">同期</span>它又学习了PyPy和Pyston,<span style="color: black;">经过</span>LLVM运行Python。你<span style="color: black;">能够</span>用一个装饰器指定你要用Numba编译的函数,Numba继承Numpy来加速函数的执行,Numba不做适时编译,它的代码是预先编译的。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p26-sign.toutiaoimg.com/pgc-image/52b57d8ea5554021a865fb52d5360d14~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723895544&amp;x-signature=9%2FbPb%2BeVOd9QRZgwjZDJ9Rqpbmk%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">PyPy</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在<span style="color: black;">选取</span>CPython的简易替代语言时,pypy无疑时最佳之选,与现有Python代码保持高度兼容性,pypy<span style="color: black;">亦</span>是默认程序运行时的一个很好<span style="color: black;">选取</span>。PyPy<span style="color: black;">运用</span>了Just-in-Time即时编译器,动态编译器与静态编译器<span style="color: black;">区别</span>,利用程序运行的过程的数据进行优化。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/3aa14dee7c8a498bad317dabaa631bac~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723895544&amp;x-signature=fIoR1Nvuvhwxy%2BLUMGEGhGm5XZ8%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">​</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">PyPy<span style="color: black;">运用</span>适时编译来加速Python,这项技术Google<span style="color: black;">亦</span>在<span style="color: black;">运用</span>,Google在V8引擎中<span style="color: black;">运用</span>它加速Javascript。<span style="color: black;">近期</span>的版本PyPy2.5<span style="color: black;">增多</span>了<span style="color: black;">有些</span><span style="color: black;">提高</span>性能的特性,其中有一项很受欢迎,它集<span style="color: black;">成为了</span>Numpy,Numpy之前<span style="color: black;">亦</span><span style="color: black;">始终</span>被用来加速Python的运行。</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">结语</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python之父说:大部分觉得Python慢的应用都是<span style="color: black;">无</span>正确地<span style="color: black;">运用</span>Python。<span style="color: black;">针对</span>CPU密集型的任务有多种<span style="color: black;">办法</span>来提升性能--<span style="color: black;">运用</span>Numpy来做计算,调用<span style="color: black;">外边</span>C代码,以及<span style="color: black;">尽可能</span>避免GIL锁。<span style="color: black;">因为</span>GIL锁<span style="color: black;">日前</span>还<span style="color: black;">没法</span>被替代,</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">彩蛋!!!</h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">必须</span>下面的资料的,还有<span style="color: black;">更加多</span>课件资源的,请私信我“666”领取!!!</h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">私信我还有<span style="color: black;">更加多</span>惊喜哦!!!</h1>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/2660b367fb794c1886689d3e5f34eb68~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723895544&amp;x-signature=Q2CDWcDLGZ1rIr66Ryxak3t1Jio%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/70f6a9271bb5426eaf08cbd1f440ae3a~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723895544&amp;x-signature=8z853gJK94%2BYe%2Fp%2Bn834KHOpZls%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/c34d62be48fa4a78b1383c8b79f816e1~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723895544&amp;x-signature=X5CtfM%2BKZYGif7kfYg%2B7loq%2BIaA%3D" style="width: 50%; margin-bottom: 20px;"></div>




情迷布拉格 发表于 2024-8-25 04:43:04

外链发布社区 http://www.fok120.com/

外链专员 发表于 2024-9-2 12:00:01

论坛的成果是显著的,但我们不能因为成绩而沾沾自喜。

j8typz 发表于 2024-10-1 20:23:59

楼主听话,多发外链好处多,快到碗里来!外链论坛 http://www.fok120.com/

qzmjef 发表于 2024-10-27 09:30:05

“BS”(鄙视的缩写)‌

wrjc1hod 发表于 2024-10-28 16:55:33

回顾历史,我们感慨万千;放眼未来,我们信心百倍。

wrjc1hod 发表于 2024-10-30 01:20:06

你的话语如春风拂面,让我心生暖意。
页: [1]
查看完整版本: Python运行速度慢?是你孤陋寡闻了?5个优化办法提速百分百