Top 10 Programming Languages to Learn in 2019

Programming

Cython vs Python – Speed up your Python

cython-vs-python-banner

Cython is an optimising static compiler for both the Python programming language and the extended Cython programming language (based on Pyrex). It makes writing C extensions for Python as easy as Python itself. In simple words, it will light speed your Python code :D. Cython will give you combined Power of C and Python.

The Cython language is a superset of the Python language that additionally supports calling C functions and declaring C types on variables and class attributes. This allows the compiler to generate very efficient C code from Cython code. Some awesome features of Cython are as follows:

It will help you to:

Why Cython is gaining a lot of traction?

There are many reasons why Python is gaining traction to solve problems in finance. Firstly, it’s relatively easy to learn. It’s also generally quicker to write programs in Python. There are also many Python libraries which are suited to analysing financial markets, such as pandas, for dealing with time series. However, one major drawback of Python is that it’ll typically run much slower than code written in languages like C. Python has the GIL (global interpreter lock), which means at any one time, just one thing can be executed at a time. The question I always get asked is how can we make Python quicker and what skills do I need to learn to do this?

One solution is Cython. In practice, Cython code is actually very similar to Python albeit with a few bells and whistles. Lots of well-known Python libraries actually use Cython. The big plus is that Cython code can be converted into fast C code and you can “release the GIL”, or in other words, you can run the code in parallel. However, you might need to spend time tuning your code and also annotating it with C type declarations. You’ll also need to compile your Cython code. However, if your project ends up using Cython extensively you’ll end up needing to spend a lot of time doing these changes.

Go through our quick introduction to python and quickly get started.

Need some multi-processing?? Cython got you covered.

If your code does a lot of downloading of data from external sources such as the web or databases, some nice choices to speed this up are Python’s threading and asyncio libraries. For more heavy-duty parallel computation you can also try the multiprocessing library. One of my personal favourites is the Celery library, which lets you run pretty much whatever Python computation you want in a distributed way. It does have a bit of steep learning curve to begin with, but understanding Celery properly is definitely time well spent in my opinion.

Cython is also the ideal language for wrapping external C libraries, embedding CPython into existing applications, and for fast C modules that speed up the execution of Python code. So if your company have a lot of legacy C code... Cython has got you covered.

The last way to speed up your Python code is probably the first thing you should try! It involves trying to vectorise your code to use libraries like NumPy and pandas, rather than overusing for loops. Very often this might actually make your code quick enough anyway.

So, yes Python isn’t the fastest language out there. However, there are solutions to help speed up the code. The bad news is there’s no free lunch, and most of the skills I’ve mentioned will require some time to learn.

If Cython has got your notice then you can download it from here. Download Cython

Rights reserved by the respective content owners.

Related posts
Programming

Amazon Price Scraper and Auto Mailer Python App

ProgrammingPython

Top 10 Deep Learning frameworks in 2019 (with comparison)

ProgrammingPython

Python vs R - Data Visualization

Programming

How Python is getting must for Traders?

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Improve Your Python

...with a fresh 🐍 Python Trick 💌
code snippet every couple of days:
You can also contribute to PyBlog
Visit Contribute to Community

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

PyBlog will use the information you provide on this form to be in touch with you and to provide updates and marketing.
%d bloggers like this: