FreeOZ论坛

标题: FastFormat:号称速度最快的C++ IO格式化库 [打印本页]

作者: coredump    时间: 7-5-2009 19:38
标题: FastFormat:号称速度最快的C++ IO格式化库
C语言中的标准输出方式printf函数族速度很快,但是一直有个让人诟病的大缺点:不安全。为此C++标准库企图用iostream来代替printf, 但是iostream不光在性能上有所不及而且在字符串格式化的灵活性方面也有点差距。作为补充,C++程序员们还可以选择Boost.Format或者Loki.SafeFormat这样的第三方C++格式化库,不过这些库在保证了类型安全的同时却在运行速度上大大落后于标准的C printf, 貌似又是一个鱼与熊掌不可得兼的遗憾........直到某个牛人写出了FastFormat。

先介绍FastFormat的作者,牛人:Matthew Wilson , 这哥们貌似澳大利亚人, 出过的技术书籍有:   Imperfect C++ (Addison-Wesley, 2004)   ,    Extended STL, volume 1 (Addison-Wesley, 2007)。目前在写的新书Breaking up the Monolith 即将在2009年由Addison-Wesley出版,这本书中介绍的几个关键C++技术都用在个本文这个FastFormat库上。

那么号称FastFormat的库究竟有多快呢? 让我们用数字说话(FROM http://www.fastformat.org/performance.html):

Scenario 1
                                          Streams (snprintf())      IOStreams      Boost.Format      Loki.SafeFormat      FastFormat.Write
Ubuntu (x86)    GCC 4.2    0.92                             1.93                6.32                    1.69                         0.50
Ubuntu (x64)    GCC 4.1    0.84                             1.58                6.46                    2.07                         0.67
Windows (x86)VisualC++9 1.84                              5.22              14.47                   2.59                         0.84
min (worst FastFormat)      0.84                             1.58               6.32                    1.69                          0.50
max (best FastFormat)       2.10                             5.22               14.47                   2.75                         0.95
avg                                    1.49                              3.28                10.29                  2.28                         0.74

这是其中一个测试的case, 在一共7个测试中,FastFormat居然有5个case速度超过标准的C printf,其中4个速度基本上是printf的2倍,速度不及的2个cases也和printf非常接近,说实话,这让人印象非常深刻。

下面来看看Type Safe方面:
Very high robustness, including 100% type-safety. It is    more robust than:     C's Streams,    C++'s IOStreams,    Boost.Format    and    Loki.SafeFormat. Indeed, with the    FastFormat.Write    API it is impossible to write defective client code!       -- from :http://www.fastformat.org/index.html

其它方面:
1. 可扩展性,号称 Infinite extensibility
2. 国际化支持
3. 简单的语法
4. 线程安全
5. 可移植性 (可在老旧的Visual C++ 6.0上编译)


好奇这一切是如何做到的吗?详见:http://www.fastformat.org/
作者: procoder    时间: 7-5-2009 19:41
不支持CE。
作者: coredump    时间: 7-5-2009 19:52
标题: 回复 #2 procoder 的帖子
WinCE? 官方的source只支持几个主流的主机系统编译器,要用在嵌入式环境的话可以考虑用gcc或者自己hack,我觉得应该不会有很大的porting障碍。
作者: MacroJ    时间: 8-5-2009 13:55





欢迎光临 FreeOZ论坛 (https://www.freeoz.org/bbs/) Powered by Discuz! X3.2