浮點轉字符串性能比較

1.浮點轉字符串

使用 boost 1.56 裏面的 karma 自帶的測試用例,修改源碼添加對 double conversion 的支持,同時輸出結果爲 markdown 表格。double conversion 優化級別O2,測試機爲 CentOS 6.5 g++ 4.4.7。結果以下
 

[root@slave205 karma]# g++ double_performance.cpp /usr/local/lib/libdouble-conversion.a -lrt -O2
[root@slave205 karma]# for i in 123456789.0 5.0 0.123456 123456.7890123 0.0000123456 ; do echo ""; ./a.out $i ; donehtml

method time result
sprintf 1.01284 123456789.000000
iostreams 1.11144 1.23457e+08
format 2.61989 123456789.000000
karma 0.534133 1.235e08
karma (string) 0.609746 1.235e08
karma (rule) 0.589086 1.235e08
karma (direct) 0.529474 1.235e08
double_conversion ToShortest 0.206492 123456789
method time result
sprintf 0.339345 5.000000
iostreams 0.58121 5
format 1.7414 5.000000
karma 0.229682 5.0
karma (string) 0.272013 5.0
karma (rule) 0.26701 5.0
karma (direct) 0.218975 5.0
double_conversion ToShortest 0.136092 5
method time result
sprintf 0.407084 0.123456
iostreams 0.692892 0.123456
format 2.07 0.123456
karma 0.208019 0.123
karma (string) 0.246272 0.123
karma (rule) 0.253367 0.123
karma (direct) 0.213895 0.123
double_conversion ToShortest 0.182453 0.123456
method time result
sprintf 1.00817 123456.789012
iostreams 1.04606 123457
format 2.67615 123456.789012
karma 0.528678 1.235e05
karma (string) 0.566756 1.235e05
karma (rule) 0.599371 1.235e05
karma (direct) 0.512787 1.235e05
double_conversion ToShortest 0.211471 123456.7890123
method time result
sprintf 0.456531 0.000012
iostreams 0.826126 1.23456e-05
format 2.06105 0.000012
karma 0.554503 1.235e-05
karma (string) 0.624482 1.235e-05
karma (rule) 0.593442 1.235e-05
karma (direct) 0.499671 1.235e-05
double_conversion ToShortest 0.219412 0.0000123456
注:CppFormat 的浮點轉換爲調用 sprintf 因此這裏並無測試它。
       迭代一百萬次,時間單位秒
 

2.整數轉字符串

相關文章
相關標籤/搜索