Hướng dẫn matlab vs python performance

I recently switched from Matlab to Python. While converting one of my lengthy codes, I was surprised to find Python being very slow. I profiled and traced the problem with one function hogging up time. This function is being called from various places in my code (being part of other functions which are recursively called). Profiler suggests that 300 calls are made to this function in both Matlab and Python.

Nội dung chính

  • A brief history of Matlab
  • A simple path to your career in backend development
  • A brief history of Python
  • Is Python or Matlab better for beginner coders?
  • Get backend development jobs by learning CS
  • Is Python or Matlab more productive?
  • Is Python or Matlab best for a programming job?
  • Which processes images better, Matlab or Python?
  • Is Python or Matlab best for machine learning?
  • Learn backend development the right way
  • Is Matlab or Python best for data science?
  • Matlab versus Python: Which is best for deep learning?
  • Matlab versus Python: Which is best for speed?
  • Matlab versus Python: Which is best for the future?
  • Matlab versus Python: Which is best for most people?

In short, following codes summarizes the issue at hand:

MATLAB

The class containing the function:

classdef ExampleKernel1 < handle  
methods (Static)
    function [kernel] = kernel_2D(M,x,N,y) 
        kernel  = zeros(M,N);
        for i= 1 : M
            for j= 1 : N
                % Define the custom kernel function here
                kernel(i , j) = sqrt((x(i , 1) - y(j , 1)) .^ 2 + ...
                                (x(i , 2) - y(j , 2)) .^2 );             
            end
        end
    end
end
end

and the script to call test.m:

xVec=[   
49.7030   78.9590
42.6730   11.1390
23.2790   89.6720
75.6050   25.5890
81.5820   53.2920
44.9680    2.7770
38.7890   78.9050
39.1570   33.6790
33.2640   54.7200
4.8060   44.3660
49.7030   78.9590
42.6730   11.1390
23.2790   89.6720
75.6050   25.5890
81.5820   53.2920
44.9680    2.7770
38.7890   78.9050
39.1570   33.6790
33.2640   54.7200
4.8060   44.3660
];
N=size(xVec,1);
kex1=ExampleKernel1;
tic
for i=1:300
    K=kex1.kernel_2D(N,xVec,N,xVec);
end
toc

Gives the output

clear all
>> test
Elapsed time is 0.022426 seconds.
>> test
Elapsed time is 0.009852 seconds.

PYTHON 3.4

Class containing the function CustomKernels.py:

from numpy import zeros
from math import sqrt
class CustomKernels:
"""Class for defining the custom kernel functions"""
    @staticmethod
    def exampleKernelA(M, x, N, y):
        """Example kernel function A"""
        kernel = zeros([M, N])
        for i in range(0, M):
            for j in range(0, N):
                # Define the custom kernel function here
                kernel[i, j] = sqrt((x[i, 0] - y[j, 0]) ** 2 + (x[i, 1] - y[j, 1]) ** 2)
        return kernel

and the script to call test.py:

import numpy as np
from CustomKernels import CustomKernels
from time import perf_counter

xVec = np.array([
    [49.7030,  78.9590],
    [42.6730,  11.1390],
    [23.2790,  89.6720],
    [75.6050,  25.5890],
    [81.5820,  53.2920],
    [44.9680,   2.7770],
    [38.7890,  78.9050],
    [39.1570,  33.6790],
    [33.2640,  54.7200],
    [4.8060 ,  44.3660],
    [49.7030,  78.9590],
    [42.6730,  11.1390],
    [23.2790,  89.6720],
    [75.6050,  25.5890],
    [81.5820,  53.2920],
    [44.9680,   2.7770],
    [38.7890,  78.9050],
    [39.1570,  33.6790],
    [33.2640,  54.7200],
    [4.8060 ,  44.3660]
    ])
N = xVec.shape[0]
kex1 = CustomKernels.exampleKernelA
start=perf_counter()
for i in range(0,300):
    K = kex1(N, xVec, N, xVec)
print(' %f secs' %(perf_counter()-start))

Gives the output

%run test.py
 0.940515 secs
%run test.py
 0.884418 secs
%run test.py
 0.940239 secs

RESULTS

Comparing the results it seems Matlab is about 42 times faster after a "clear all" is called and then 100 times faster if script is run multiple times without calling "clear all". That is at least and order of magnitude if not two orders of magnitudes faster. This is a very surprising result for me. I was expecting the result to be the other way around.

Can someone please shed some light on this?

Can someone suggest a faster way to perform this?

SIDE NOTE

I have also tried to use numpy.sqrt which makes the performance worse, therefore I am using math.sqrt in Python.

EDIT

The for loops for calling the functions are purely fictitious. They are there just to "simulate" 300 calls to the function. As I described earlier, the kernel functions (kernel_2D in Matlab and kex1 in Python) are called from various different places in the program. To make the problem shorter, I "simulate" the 300 calls using the for loop. The for loops inside the kernel functions are essential and unavoidable because of the structure of the kernel matrix.

EDIT 2

Here is the larger problem: https://github.com/drfahdsiddiqui/bbfmm2d-python

Matlab and Python both can do quite different and incredible things, which makes the question of “Matlab versus Python” interesting. When considering Matlab or Python, the best question to ask is which is best for you?

No matter if you’re just starting out coding and trying to decide which language is best to pick up, or you already have five languages under your belt and you’re wondering which should be the sixth, it can be hard to choose the right one to pursue. There are new awesome languages coming up all the time, like Rust. There are more niche languages like R that are expanding their capabilities. And of course, there are Matlab and Python to choose between as well.

If you find yourself in that boat, this article is for you.

A brief history of Matlab

Matlab, which is an abbreviation of “Matrix Laboratory”, is a prorietary programming platform that is designed for engineers and scientists to create and analyze systems and products. Originally , it wasn’t even a language at all - it was a simple interactive matrix calculator. It’s also a commercial product and comes with its own working environment. Historically this was seen as an advantage - large companies and educational institutions love having someone to call when things break. These days, nearly all programming languages used in produciton are open-source and free.

Like we mentioned, Matlab is not an open-source product, which means you don’t have access to the inner workings of its proprietary code. It dominates in academic settings, where its computing power makes it popular for fields with a numeric component. However, it can be extremely expensive for everyday consumers to buy even a single license.

Originally developed by math magician and computer programmer Cleve Moler in the 1960s, Moler didn’t sell it originally. At the time, it was distributed for free to universities. It wasn’t until the 1980s when it was released as a commercial product.

A simple path to your career in backend development

Hướng dẫn matlab vs python performance

The pace of Boot.dev's JavaScript, Python and Go courses has been perfect for me. The diverse community in Discord is a blast, and other members are quick to help out with detailed answers and explanations.

- Daniel Gerep from Cassia, Brasil

A brief history of Python

Python is a very different language. It is officially defined by Wikipedia as “an interpreted high-level general-purpose programming language.” It’s open-source, which means anybody can access it, dig around in the guts to see how it works, and even create their own packages for it. This is one of the major advantages of Python over Matlab.

Python was ideated in the late 1980s and was first implemented in December 1989. It’s been around since then, growing more popular, and consistently ranks as one of the most popular languages today. It’s got a huge and growing functionality. It’s good for simple tasks that beginners might be interested in, and complex tasks that organizations might want.

Is Python or Matlab better for beginner coders?

If you’re a brand-new coder and you’re wondering whether you should learn Matlab or Python, the answer is probably Python, even if you have free access to Matlab through your University or other organization.

A case could be made for both, but if you’re primary goal is to learn to code as opposed to learning mechanical engineering, for example, then Python is the more useful tool.

Matlab is good for engineers who are mainly interested in using it as a simple coding tool to perform classical engineering calculations. Because of the built-in integrated development environment and debugger, it makes it easy for non-coders to write executable logic. Honestly when you get started coding, one of the hardest things is getting to grips with the environment. Python has a steeper learning curve especially for people who have never coded a line before.

However, Python is also great for beginners when compared to most programming languages. On any coding survey, Python typically comes out on top as the best language for beginners to learn. The benefits of Python include tons of free resources to help you learn; the simple grammar and syntax that reads like English; and the rapid gratification in creating your first functional program, which can happen in just a few minutes.

Both Matlab and Python are good for beginners, but Python is better if coding or computer science is your primary goal.

Get backend development jobs by learning CS

I was a field service engineer and I wanted to become a backend developer, but work and family limited my options. Since completing the backend-focused computer science track on Boot.dev, I now have a job offer in hand and I'm starting my career as a software developer!

- Özgür Yildirim from Germany

Is Python or Matlab more productive?

What about productivity? If you’re an engineer who takes their output efficiency seriously, you may wonder whether Matlab or Python is better for that.

If you’re a beginner coder and you care about productivity I would recommend Python. The clean syntax is easy to read and write, which means you can get more done.

It’s possible that if you’re more specialized in the mathematical and classical engineering world that Matlab might help you become more productive. It comes with a really clean interface and familiar mathematical notation. That said, even programming adjacent fields like electrical and mechanical engineering, which Matlab has historically dominated, are moving towards Python. Anecdotally, my brother-in-law who is studying chemical engineering at university said his entire department has moved their classed from Matlab to Python.

If you’re a beginner, Python will help you be more productive. If you’re specialized in the technical computing world, Matlab’s tidy interface will boost your productivity.

Is Python or Matlab best for a programming job?

Many new programming students pick up a keyboard because they want to get into computer science. If that’s the case for you, then there’s only one answer for which is best between Matlab and Python: Python is often considered the lingua franca for computer science. If you want to get a job in computer science, scan any of the job postings and you’ll see most if not all require some knowledge in Python. Hardly any will mention Matlab as even a benefit, let alone a requirement.

Plus, Matlab is mostly used in schools and universities. This means that if you want to learn Matlab once you’ve left school, you’ll find it really expensive. Python, meanwhile, has a much wider array of courses and tutorials that can teach you anywhere from the basics to pretty advanced stuff.

Python is indubitably best for getting a job in computer science.

Which processes images better, Matlab or Python?

Image processing is a method to extract or enhance an image. While it was first used to enhance lunar images in the 60s, today it has a much wider use range , including image correction, social media filters, medical technology, computer vision, and pattern recognition.

Matlab is really good at designing and fine-tuning algorithms. It offers a bunch of toolboxes that can help you process images. The toolbox for image processing in Matlab means you can use it for image data segmentation, extraction, and analysis.

Python does offer many external packages for image processing, like Numpy and SciPy. However, analyticsInsight.Net found that while both Matlab and Python were able to produce the same results, Matlab was faster.

While Python handles image processing just fine, Matlab has native toolboxes designed for image processing, and it’s faster.

Is Python or Matlab best for machine learning?

Machine learning makes the internet you use a lot more user-friendly. It makes internet search queries more relevant, it helps email clients filter out spam, and it also plays a big role in websites selling you personalized recommendations. I’m writing this article using voice to text, which uses machine learning to increase accuracy. All this to say, ML it has a lot of uses, and I expect it will only have more as time goes on.

This section has an easy answer: Python is the winner over Matlab. Python has tons of libraries and packages for both old school and new school machine learning models. Plus, Python is the most widely used language for modern machine learning research in industry and academia. Manie Tadayon said it best in his article :

“[Machine learning] is the area where Python and R have a clear advantage over Matlab.”

Matlab can be used for machine learning. Matlab has a large collection of algorithms that can offer immediate visual feedback. But remember, those algorithms are proprietary. There’s very little code portability. While it’s easier to get started with machine learning, it doesn’t really have a future unless you only work with other people who use Matlab.

Matlab can be used for machine learning, but Python is both better and more portable.

Learn backend development the right way

Computer science skills will get you interviews. A portfolio of projects will get you hired.

Is Matlab or Python best for data science?

Data science is such a wide field that you can find convincing arguments for Python being best, or Matlab being best, or both being terrible. Even this section could be its own article because once again, it depends on what you want to do with data science.

For example, this article tells you that python has a really steep learning curve. Matlab, by comparison, is better for fledgling data scientists. Again, it depends on what you want to do with the data science products you produce. Do you want to show them to other people? You won’t be able to do that with Matlab. It also doesn’t really explain how you can get access to Matlab if you’re not part of a university. Remember, proprietary licenses are expensive.

If you’re currently going to school somewhere where you have access to Matlab, and you just want to get your feet wet with data science concepts, I can see how Matlab would be a great option for you. However, if, like a lot of future data scientists, you find yourself wanting to learn data science after you leave school, the only real option is Python. And it’s a great option!

Python is comparatively easy for beginners to learn. It’s open-source, which means there’s a rich array of tutorials and examples that other people have created for you to walk through. There are tons of Python projects for beginners you can do if you really want to get invested in learning a language. The community is diverse and very varied. When choosing Python vs Matlab for data analysis, Python wins.

While it may be tougher to pick up Matlab, I still consider Python a great option for data science.

Matlab versus Python: Which is best for deep learning?

Deep learning is considered an evolution of machine learning. According to Zendesk’s helpful blog post , deep learning “uses a programmable neural network that enables machines to make accurate decisions without help from humans.” You can consider it a subfield of machine learning. Theoretically, deep learning models should be able to determine on their own if predictions are accurate. Machine learning requires more human guidance.

That’s part of the reason I consider Python the best option for deep learning. Because Matlab is proprietary, it means innovations are slower. Python is open-source, which means anybody can create packages that become widely adopted by the Pythonista community. Packages like PyTorch, Tensorflow, Caffe, and so on are widely used for deep learning. If you have a look at most deep learning online courses, they all feature Python.

In summary, Python is better than Matlab for deep learning.

Matlab versus Python: Which is best for speed?

I alluded to this above, but MatLab is really fast. There are a couple of functions that are known to slow down Matlab, like fitlm, but as long as your code avoids those functions, Matlab is indubitably faster than Python.

However, it’s worth noting that Python does compare favorably to Matlab with the ability to use more than 12 processing cores when running jobs in parallel. I loved Rob Hicks’s writeup of the question where he ran time-consuming econometric problems in Python and Matlab. He wrote all the code, so you can check it out for yourself.

Matlab is faster than Python, but Python is better at running multiple jobs in parallel.

Matlab versus Python: Which is best for the future?

Programming languages come and go like teenage romances. Even those that seem permanent can start to lose sway, and newcomers challenge existing languages all the time. Despite the fact that this causes learning languages to be more complicated, because you never know if spending 5 years learning Rust is a good idea or not, I think it is a good thing. Innovation, especially in fields like computer science and data science, is absolutely critical for progression. If a language comes along that can do something better than Python, even though I love Python, I would still be happy to see that.

That’s why considering a language’s future-proof-ness is really important to me. While Matlab has been around longer, I humbly believe that open-source languages are the future. Paywalled languages and interfaces won’t go far as data science and computer science become more democratized. We see this in the acceleration of applications for Python – MatLab just can’t keep up because it’s not open source.

Python has demonstrated its ability to overcome a lot of obstacles, including the Python 2 to Python 3 fiasco, the retirement of its benevolent dictator for life, and the rise of trendier languages that challenged its throne. Matlab, meanwhile, has waning influence even in the areas where it once dominated like academic circles.

My college class was the first year to be taught R over Matlab because our professors believed it was a more useful language. And when I wanted to learn a new language, I couldn’t choose Matlab because I couldn’t afford it. Python was a natural choice**.**

Python is here to stay. Matlab will only become less relevant.

Matlab versus Python: Which is best for most people?

I normally try to avoid being too decisive in these types of articles, because the truth is there rarely is one best language over another. But I believe this is one of the few cases where one language is clearly superior to the other for a variety of reasons. Matlab has a couple of useful applications that make it a good choice for a very narrow group of people. Python, meanwhile, is only growing more useful to more people. There are only going to be more tutorials and more guides to learn it and use it.

I’m not saying you shouldn’t learn Matlab if you get the opportunity. (And if you’re really sold on Matlab, there’s even an option to use Matlab for Python!) But I think most people won’t have that chance. when it comes to choosing between Matlab versus Python, Python wins.