In college, I had a physics class called Modeling and Simulation of Physics Systems in which students wrote code to model the physical behavior of various systems. The final project of the class involved choosing a system of interest, developing a model for it, and presenting the results with some sensatory aid. Having an interest in music and guitars, I chose to simulate strumming the strings of a guitar with the ultimate goal of playing the string-displacement data as a sound. Students were allowed to use any language they wished, as the class was not teaching computer languages. I initially chose to use Python, as it is very easy to use and was (and still is) my favorite language.
Regardless of language, the simulation involved breaking guitar strings up into small pieces and using known mathematical equations for a wave traveling along a string as a function of position and time to simulate the movement of each piece. As with any simulation, the accuracy of the model can be improved by making the pieces and time steps smaller. But as those become smaller, the time it takes to run the simulation increases. I wanted to have an accurate simulation in which I could simulate all six strings and generate and play a sound wave multiple times during the course of a short presentation. This meant that the simulation would have to run in a matter of seconds.
After writing the simulation in Python and setting physical parameters to get the accuracy I desired, I ran the simulation for one string…and it took nearly a minute to run…for one string… Thus, it would take an eternity (with respect to an audience) to simulate all six strings and play a resulting sound. This was unacceptable and I worried that there might not be a way to achieve my goal…at least not with Python alone. I decided I would investigate using some other computer language.
The next language I decided to try was Mathworks Matlab. I basically copied my code from Python and restructured the syntax to make it work in Matlab. I suspected that my simulation would run faster, but I had no idea how much faster. The result blew me away. Instead of taking a minute to run, the simulation completed in a matter of seconds! This was excellent and I could probably accomplish my goal with this, although it would still add up to a significant chunk of time with simulating six strings. Running my simulation code in Matlab reduced the run time so significantly that I became curious as to how fast it might run in a lower-level language such as C++. Thus, I rewrote my code once again and ran the simulation. This time the same code took only a fraction of a second to run!
I initially chose to use Python, as it was my favorite language, and it is very easy and quick to use. It didn’t take very long to write the code for the simulation, but there is a lot of overhead execution going on under the hood of Python that can make running very computationally-intensive programs inefficient. Lower-level languages such as C/C++ may be more difficult to use, but they provide little overhead, which can greatly reduce the execution time of a program. It really depends on the task, though, as to whether or not the overhead of a language will have such an impact. It is definitely something to keep in consideration when starting a project that doesn’t have a language specified.
More from my site