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 ; done
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.整数转字符串