Hello guys,
Today, I’ll be demonstrating a short but significant topic. There are widespread facts that, on many occasions, Python is relatively slower than other strongly typed programming languages like C++, Java, or even the latest version of PHP.
I found a relatively old post with a comparison shown between Python and the other popular languages. You can find the details at this link.
However, I haven’t verified the outcome. So, I can’t comment on the final statistics provided on that link.
My purpose is to find cases where I can take certain tricks to improve performance drastically.
One preferable option would be the use of Cython. That involves the middle ground between C & Python & brings the best out of both worlds.
The other option would be the use of GPU for vector computations. That would drastically increase the processing power. Today, we’ll be exploring this option.
Let’s find out what we need to prepare our environment before we try out on this.
Step – 1 (Installing dependent packages):
pip install pyopencl pip install plaidml-keras
So, we will be taking advantage of the Keras package to use our GPU. And, the screen should look like this –

Once we’ve installed the packages, we’ll configure the package showing on the next screen.

For our case, we need to install pandas as we’ll be using numpy, which comes default with it.

Let’s explore our standard snippet to test this use case.
Case 1 (Normal computational code in Python):
############################################## #### Written By: SATYAKI DE #### #### Written On: 18-Jan-2020 #### #### #### #### Objective: Main calling scripts for #### #### normal execution. #### ############################################## import numpy as np from timeit import default_timer as timer def pow(a, b, c): for i in range(a.size): c[i] = a[i] ** b[i] def main(): vec_size = 100000000 a = b = np.array(np.random.sample(vec_size), dtype=np.float32) c = np.zeros(vec_size, dtype=np.float32) start = timer() pow(a, b, c) duration = timer() - start print(duration) if __name__ == '__main__': main()
Case 2 (GPU-based computational code in Python):
################################################# #### Written By: SATYAKI DE #### #### Written On: 18-Jan-2020 #### #### #### #### Objective: Main calling scripts for #### #### use of GPU to speed-up the performance. #### ################################################# import numpy as np from timeit import default_timer as timer # Adding GPU Instance from os import environ environ["KERAS_BACKEND"] = "plaidml.keras.backend" def pow(a, b): return a ** b def main(): vec_size = 100000000 a = b = np.array(np.random.sample(vec_size), dtype=np.float32) c = np.zeros(vec_size, dtype=np.float32) start = timer() c = pow(a, b) duration = timer() - start print(duration) if __name__ == '__main__': main()
And, here comes the output for your comparisons –
Case 1 Vs Case 2:

As you can see, there is a significant improvement that we can achieve using this. However, it has limited scope. Not everywhere you get the benefits. Until or unless Python decides to work on the performance side, you better need to explore either of the two options that I’ve discussed here (I didn’t mention a lot on Cython here. Maybe some other day.).
To get the codebase you can refer the following Github link.
So, finally, we have done it.
I’ll bring some more exciting topic in the coming days from the Python verse.
Till then, Happy Avenging! 😀
Note: All the data & scenario posted here are representational data & scenarios & available over the internet & for educational purpose only.
You must be logged in to post a comment.