суббота, 1 марта 2008 г.

Comparison of string performance using C#,C++(STL,boost) and C - part 3, results

Previous parts:
Compare string performance using C#,C++(STL,boost) and C - part 1, C#
Compare string performance using C#,C++(STL,boost) and C - part 2, C\C++

This comparison was inspired by STL vector performance

Task -
you have array of strings "word1-word2", "word3-word4", "word5-word6", you need to transform it to string "word1:word2:word3:word4:word5:word6"


I was doing this on Windows so I use QueryPerformanceCounter to test performance to keep performance testing the same with C# and C++.

Goal - compare string performance on plain C with permormance of C++ code using STL and boost. I try to be as fear as possible and select "real-life"-like task. I add C# performance only when I notice difference between plain C and "C++ with STL".

For C/C++ I use VS 2005, release version, speed optimization. For C# I use VS 2008, Express Edition. My computer is Athlon 64 2-core 2.8 GHz, 2Gb of ram. Windows Vista.

Results, sorted by speed (I do many test runs and this is "average" results for 10000 iterations)






Boost String Algorithms Library0.596665 sec
select_many0.100862 sec
boost::tokenizer0.086117 sec
"naive" C++0.053814 sec
C#0,041314 sec
C0.003925 sec


Conclusion - C code is still 10 times faster then C# code, and still may be a bit optimized. But I didn't optimize C# code, even more, I try to write it in modern C#3.0 style using lambda/generics. When I rewrite C# code using StringBuilder it was 4 times slower then C code.

As for C++ code ("naive" or with boost) - either I don't familiar with some secret STL optimization technics...or...

And results using Boost String Algorithms Library looks really bad.

I see that C code use different algorithm then C++ or C# code. But this was C "idiomatic" way, again at least as I understand it.
Also I see that C code is not "general" enought, it will fail on strings like "word1--word2","word1-word2-". But my goal was to solve the problem. Pre-generalization is almost as bad as needless pre-optimization.

1 комментарий:

Анонимный комментирует...

Bad comparison, uses as the separator character to "c" and "c#" while "c + +" separator is a string. If you adjust the 'c++' boost...