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 much faster is Cython than Python?

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

Share

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

This article mainly explains "how much faster is Cython than Python". Interested friends might as well take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how much faster Cython is than Python.

Many people compare Python with C only in terms of performance, but not in terms of rapid development, and begin to complain that Python is too slow. The advantage of Python is that its development time is very short, and you don't have to deal with pointers, memory management, and so on.

In most cases, development time is more important than performance. Python has a great community with a lot of good libraries and a lot of needs can be met there.

That said, what do you do if you want to make your Python code faster? Let's try Cython!

Cython: C extension of Python

If you like both the simple syntax of Python and the high performance of C, then Cython is what you want. You can use Cython to write C extensions for Python, and the code will be converted to optimized C / C + code. It can provide high performance and can be used in Python projects.

Install Cython

This requires a C compiler, which you can install according to your operating system, and then install Cython with the following command:

Pipinstall Cython

Benefit from static type declaration

Static type declarations can be added to variables and functions, which will provide better performance. You can use all C types such as int,float,double for declaration.

Declare variables in Cython

Cdef int i = 10

The basic meaning of this command is to declare a variable I, which is an integer.

Declare a function in Cython

Cdef intsquare (int x): return x * * 2

This command basically means that the square function takes the integer x as an argument and returns the integer value x * * 2, which is the square of x. When the module is imported, the functions declared by cdef are not visible to the Python code. To make it visible, you need to use cpdef instead.

Calculating factorial in Python

Write a Python function to calculate the factorial of a given integer:

Effactorial (n): if n > = 1: return n * factorial (n-1) return1

Using Cython to make Python faster

First, create a pyx file.

Cpdef longfastfactorial: this function will return a long value, so we declare it to be of type long by placing long before the function name fastfactorial.

Long n: we expect n to be a long value.

Cpdef long fastfactorial (long n): if n > = 1: return n * fastfactorial (n Mel 1) return1

Next, we need to create a setup.py file to convert Cython to C.

From distutils.core import setup fromCython.Buildimport cythonize setup (ext_modules = cythonize ('fastfac.pyx'))

Compiled code

To perform the compilation, you need to run the following command:

Python3setup.py build_ext-inplace

-- inplace: this option creates a shared object file in your working directory.

Contrast

Let's compare the two:

From fastfac import fastfactorial from fac import factorial from timeit import timeit print (timeit ('fastfactorial (20)', globalsglobals=globals (), number=10000) # output: 0.002164322999306023 print (timeit ('factorial (20)', globalsglobals=globals (), number=10000)) # output: 0.18900858898996376

The result is amazing, Cython is nearly 88 times faster!

At this point, I believe you have a deeper understanding of "Cython is much faster than Python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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