中文

用 Python 来生产: 无需重写代码,即可加速运行

你可能一直在使用 Python 来进行数据科学原型和数据处理工作,但是当把代码投入实际生产时,你需要一些 C 的变种版本。这是为什么呢?从本质上讲,Python 是单线程的,全局锁(GIL)是主要的罪魁祸首,迫使在启动另一个线程之前需要先等待完成一个线程。最终的效果是生成一个非常棒的解释器,但涉及到大型工作时,它可能比糖浆还慢。

大多数 Python 使用者不知道的是,Python 代码的性能瓶颈已经基本消除,根本不需要改变 Python 代码!您可以获得机器语言般的执行速度,解除 GIL 全局锁,并通过利用由 Intel 创建的一组库和工具来使 Python 几乎可以在每个项目中都利用到多线程和多核的优势。

首先,Intel® Python发行版*(IDP)具有大量的软件包可以帮助加速 Python 代码执行速度,它们通过使用 C 函数将 Python 代码的实际执行完全脱离 Python 层,然后调用处理器向量化和并行化以提高性能并缩短执行时间 – 通常达到许多个数量级。

Intel®线程构建模块(TBB)是 OpenMP 的替代产品,它提供编译后的操作系统代码,以便在用户请求并行时分配工作、保持高速缓存热度并平衡负载。TBB 识别它可以拆分的部分并分配给 2 个或更多内核,其代码位于最底层,因此可以进行嵌套调用。然后是 Intel®数据分析加速库(DAAL),该技术可加快流线化和分布式算法的传统机器学习和数据科学工作流程,并与 Intel 库软件包的其余部分完全集成。此外,还针对 Intel CPU 上的 Python 进行了优化,包括 NumPy 等软件包,该软件包使用 Intel®数学核心函数库(MKL),SciPy(也是使用 MKL 的标准科学工具集)和 numba – 一种即时编译器,它允许最新的 SIMD 特性和多核执行程序充分利用当今的 CPU 的机能。这些只不过是您可以在 Intel®分布式Python *版中找到的软件包的冰山一角。

一旦找到合适的加速工具,就需要调整代码以获得最佳性能。也许嵌套循环令代码进入死循环并阻止多个线程执行,或者出现其他一些编码瓶颈问题。如果只有你确切知道哪一行代码是罪魁祸首。那么,Intel 也有这样的工具,以 Intel®VTune Amplifier™为例,这是一种基于 GUI 的分析工具,可以自动检测混合的 Python / C 代码,识别热点,并提供完整的调用堆栈列表以显示随时间推移的性能。它仅花费大约 15% 的开销,并且不需要修改任何代码。它显示了多个线程正在运行的时间、在特定时间内有多少个内核处于活动状态,并通过自下而上或自上而下的方式查明问题,您可以通过一个直观且易于使用的界面来使用这个工具。

一个更好的消息是,你可以现在免费下载它

毕竟,谁会比 Intel 更了解如何充分优化自家的处理器性能呢?

Dr. Michael J. Garbade

I, Dr. Michael J. Garbade is the co-founder of the Education Ecosystem (aka LiveEdu), ex-Amazon, GE, Rebate Networks, Y-combinator. Python, Django, and DevOps Engineer. Serial Entrepreneur. Experienced in raising venture funding. I speak English and German as mother tongues. I have a Masters in Business Administration and Physics, and a Ph.D. in Venture Capital Financing. Currently, I am the Project Lead on the community project -Nationalcoronalvirus Hotline I write subject matter expert technical and business articles in leading blogs like Opensource.com, Dzone.com, Cybrary, Businessinsider, Entrepreneur.com, TechinAsia, Coindesk, and Cointelegraph. I am a frequent speaker and panelist at tech and blockchain conferences around the globe. I serve as a start-up mentor at Axel Springer Accelerator, NY Edtech Accelerator, Seedstars, and Learnlaunch Accelerator. I love hackathons and often serve as a technical judge on hackathon panels.

Recent Posts

Blockchain in Elections: A Leap Toward Transparent Democracy

In 2024 we're witnessing a critical point in democratic technology: the integration of blockchain and…

4 months ago

Win Big with Our Amazon Fire Max 11 & AirPods Pro Giveaway!

We’re thrilled to announce an exciting opportunity for you to win not one but two…

6 months ago

Unleashing Potential: How Education Ecosystem Transforms Learning into Real-World Success

Acquiring practical skills is crucial for career advancement and personal growth. Education Ecosystem stands out…

8 months ago

The Role of Artificial Intelligence in Modern Software Development

Artificial Intelligence (AI) has been making significant strides in various industries, and the software development…

11 months ago

Highest Stable Coin Yields – (W16 – 2024)

Another week to bring you the top yield platforms for three of the most prominent…

12 months ago

LEDU Token OTC Trading

If you hold a large volume of LEDU tokens above 1 million units and wish…

1 year ago