1

I want to create a special case of a room-impulse-response. I am following this implemetation for a room-impulse-response generator. I am also following this tutorial for integrating c++\c with python.

According to the tutorial:

  1. You want to speed up a particular section of your Python code by converting a critical section to C. Not only does C have a faster execution speed, but it also allows you to break free from the limitations of the GIL, provided you’re careful.

However, when looking at the MATLAB example, all I see the cpp code segment doing, are regular loops and mathematical computations. In what way will c\cpp be faster than python\MATLAB in this example or any other? Will any general c\cpp code run faster? If so, why? If not, what are the indicators I need to look for, when opting for a c\cpp segment implementation? which operations are faster in c\cpp?

havakok
  • 1,185
  • 2
  • 13
  • 45
  • 1
    This is too broad to answer fully. Whether anything is interpreted or compiled is the least of your concerns when looking to optimize python or matlab. A simplistic rule of thumb is you may want to call to C++ if you see lots of loops dominating your run time. – Passer By Jan 16 '22 at 14:32

1 Answers1

1

Why use C++ to speed up Python

C++ code compiles into machine code. This makes it faster compared to interpreter languages (however not every code written in C++ is faster than Python code if you don't know what you are doing). in C++ you can access the data pointers directly and use SIMD instructions on them to make them multiple times faster. You can also multi-thread your loops and codes to make them run even faster (either explicit multi-threading or tools like OpenMP). You can't do these things (at least properly) in a high level language).

When to use C++ to speedup Python

Not every part of the code is worth optimizing. You should only optimize the parts that are computationally expensive and are a bottleneck of your program. These parts can be written in C or C++ and exposed to python by using bindings (by using pybind11 for example). Big machine learning libraries like PyTorch and TensorFlow do this.

Dedicated Hardware

Sometimes having a well optimized C++ CPU code is not enough. Then you can assess your problem and if it is suitable, you can use dedicated hardware. These hardware can go from low-level (like FPGA) to high-level hardware like dedicated graphics cards we usually have on our system (like CUDA programming for NVIDIA GPUs).

Regular Code Difference in Low and High Level Languages

Using a language that compiles has great advantages even if you don't use multi-threading or SIMD operations. For example, looping over a C array or std::vector in C++ can be more than 100x faster compared to looping over Python arrays or using for in MATLAB (recently JIT compiling is being used to speed up high-level languages but still, the difference exists). This has many reasons, some of which are basic data types that are recognized at compile time and having contiguous arrays. This is why people recommend using Numpy vectorized operations over simple Python loops (same is recommended for MATLAB).

Shahriar
  • 768
  • 4
  • 11
  • In the case of the given RIR example, no multithreading method was used. Also, I can not recognize any simd operations (did I miss any?). Does that mean that in this case the code is not faster\senificantly faster? Or is the pointer usage enough to lower the computational complexity? – havakok Jan 16 '22 at 12:00
  • 1
    @havakok I updated my answer to answer your comment too. – Shahriar Jan 16 '22 at 12:34
  • Got it. Thanks a lot. – havakok Jan 16 '22 at 12:40
  • Being interpreted has very little to do with how terribly slow python is. – Passer By Jan 16 '22 at 14:27
  • Yes there are many factors here. But I think that python by itself is not supposed to be fast. I think that it is supposed to be a wrapper around C++ that makes things easy. – Shahriar Jan 16 '22 at 14:59
  • `looping over a C array or `std::vector` in C++ can be more than 100x faster compared to […] using `for` in MATLAB.` This was true 20 years ago, but is absolutely not true today. MATLAB compiles code before running it. It’s called “Just in Time compilation”. There is still a difference in execution speed, but more in the order of 2x. – Cris Luengo Jan 16 '22 at 15:11
  • @CrisLuengo I haven't used MATLAB in recent years but I remember when I was using it 3-4 years ago, the difference was huge. But yeah JIT is being used more and more in high-level languages. I'll edit the answer. – Shahriar Jan 16 '22 at 15:29
  • Python is not 'supposed to be a wrapper around C++ that makes things easy'. But it is the looping that gets a big performance boost when using C/C++. The compiler will optimize for performance quite a lot better than anything Matlab or Python can do. Also, the math functions (`pow` and trig functions in particular) are very optimized in the STL. @havakok, having said that you probably can get away with implementing the impulse response computations in Python if it is not meant for real-time. And for the record, you can multi-thread in python. – dmedine Jan 17 '22 at 00:39
  • @dmedine I said `I think that it is supposed to be a wrapper around C++` (emphasis on I think). This is the way I sometimes see it. – Shahriar Jan 17 '22 at 06:43