Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make Python run as fast as Julia

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to make Python run as fast as Julia, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Comparison between Julia and Python

Should I discard Python and other languages and use Julia to perform technical calculations? After seeing the benchmark on http://julialang.org/, people are bound to think so. Python and other high-level languages lag far behind in speed. However, the questions that come to mind are different: can the Julia team write Python benchmarks in the way that works best for Python?

My view of this cross-language comparison is that benchmarks should be defined based on the tasks to be performed, and then the language experts should write the code to perform those tasks. If the code is written entirely by a language team, there is a risk that other languages will not be used.

One thing the Julia team did right was that they released the code they used to github. Specifically, the Python code can be found here.

* seeing this code confirms the bias I'm afraid of. The code is written in C style and uses a lot of loops on arrays and lists. This is not the way to use Python.

I don't blame the Julia team because I feel guilty about having the same bias. But I learned a cruel lesson: avoid loops on arrays or lists at any cost, because they do slow down in Python, see "Python is not C".

Given this bias against the C style, an interesting question (at least for me) is whether we can improve these benchmarks and make better use of Python and its tools.

Before I give an answer, I would like to say that I will never try to belittle Julia. After further development and improvement, Julia is undoubtedly a language worth paying attention to. I just want to analyze things about Python. In fact, I'm using this as an excuse to explore various Python tools that can be used to make code run faster.

In the following content, I use the Docker image to use Python 3.4.3 in Jupyter Notebook, where all the Python scientific tool combinations are installed. I will also use Anaconda to run the code through Python 2.7.10 on the Windows machine. Timing is performed for Python 3.4.3. The Notebook containing the complete code for all of the following benchmarks can be found here (https://www.ibm.com/developerworks/community/blogs/jfp/resource/julia_python.zip).

Given the various comments on social media, I added the following sentence: I am not using an alternative implementation of Python here. I didn't write any C code: if you don't believe me, try looking for a semicolon. All the tools used in this article are standard Cython implementations provided with Anaconda or other distributions. All of the following code runs in a single Notebook.

I've tried using the Julia micro-performance file from github, but I can't run it intact using Julia 0.4.2. I have to edit it and replace @ timeit with @ time before it can run. Before timing them, I must also add a call to the timing function, otherwise compilation time will be included. The file I use is located here. I run it using the Julia command line interface on the same machine that I used to run Python.

Chronological code

The * benchmark used by the Julia team is a simple code for the Fibonacci function.

Def fib (n): if n

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report