[Update2] I used the library eigen to reimplement the algorithm, reducing the code from 84 lines to 41 lines (wow), and time increases 11%. Looks acceptable, with similar workload as unoptimized C#, but the same speed as heavily optimized C# (with 164 lines of code and unsafe compilation). What's even better is eigen is a header library requires no pre-building or linking.
Looked at Boost for a while yesterday and also found similar usage of lambda expression and smart pointers as in C#. Considering C++ as an option for the future seriously...
[Update] I further optimized the C# code and made its speed from 204ms to 124ms. The same trick made C++ implementation from 124ms to 105ms. This makes me prefer C# more. The code is also updated on github.
Please consult the prezi slides. Basically the conclusions are:
- MATLAB sort of astonishes me. I guessed it should be the fastest with the sophisticatedly optimized matrix library.
- For loops turn to be "speed killer" in MATLAB and Python. On the contrary, C family languages are able to optimize such loops well.
- It's also a bit surprising to see C# only costs twice the time as C++. But only limited to tedious imperative style without using any libraries.
- Functional style (map for Python, arrayfun for MATLAB, LINQ for C#) and imperative style (for for all the three languages) make no difference for MATLAB, but C# prefers imperative style, while Python favors functional style.
- I'd use C# for its flexibility from dev speed to running speed.
Code is available on github.
Comments