Yak!の日記: Expression Template による文字列の連結 - 結果
ふと思い立って、Expression Template による文字列の連結を書いてみる。 こんなの間違いなく誰かがやっているはずと思ったら案の定kMonos.NETさんのところにあるわけだが、まぁやっちゃったものは仕方ない。
で、gcc version 3.4.4 (cygming special) (gdc 0.12, using dmd 0.125)で、-O2 でコンパイル、実行した結果は以下の通り。
[statically allocated buffer with optimized C function]
hogehogehogefoobarzotaiueokakikukekoabcdefghijklmn 0.1942
[statically allocated buffer with standard C function]
hogehogehogefoobarzotaiueokakikukekoabcdefghijklmn 0.3085
[dynamically allocated buffer with optimized C function]
hogehogehogefoobarzotaiueokakikukekoabcdefghijklmn 0.744
[dynamically allocated buffer with standard C function]
hogehogehogefoobarzotaiueokakikukekoabcdefghijklmn 0.8613
[C++ string with expression template (without clear)]
hogehogehogefoobarzotaiueokakikukekoabcdefghijklmn 0.2904
[C++ string with expression template (with clear)]
hogehogehogefoobarzotaiueokakikukekoabcdefghijklmn 1.632
[C++ string with compound assignment operator]
hogehogehogefoobarzotaiueokakikukekoabcdefghijklmn 11.377
[C++ string with add operator]
hogehogehogefoobarzotaiueokakikukekoabcdefghijklmn 15.732
without clear はメモリのサイズ調整がないというずるい条件なので、同じくメモリを動的に確保しない場合の C と同程度の処理時間である。一方、with clear の方は C に比べて 2 倍程度の処理時間、素直に + 演算子で書いた場合の 1/10 とかなり健闘している。もっとも「Expression Template が頑張っている」というより「駄目じゃん C++」という気になるかもしれないが。いずれにしろ、メモリ確保が結構重い処理であること、Expression Template によって手軽に軽減できることは確かである。もっとも、処理時間の桁が違っているとは言っても100万回まわしてこれなので、文字列の連結ばっかりやってるようなケースでもなければ実質的な効果はないかもしれない。
Expression Template による文字列の連結 - 結果 More ログイン