这更快,而且更灵活:printf或cout? [英] which is faster, and which is more flexible: printf or cout?

查看:146
本文介绍了这更快,而且更灵活:printf或cout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能的重复项:

printf vs cout in C ++

cin或printf?

我一直想知道printf和cout ..更快,也是最灵活的(即可以打印一系列变量,并且输出可以格式化)?

I've always wondered about printf and cout.. which one is ultimately faster, and is it the most flexible as well (ie can print a range of variables, and output can be formatted)?

PS
我知道这看起来类似于 http://stackoverflow.com/questions/2872543/

P.S. I know this looks similar to http://stackoverflow.com/questions/2872543/printf-vs-cout-in-c ,but i'm not really asking the same thing.

推荐答案

短回答



更快速: printf

cout

sprintf系列,C ++流应该是较慢的(如果我记得一个特例C ++,Herb Sutter的一个因素6)。仍然,大多数时候,你不需要这个速度,但你需要确保你的代码不会被窃取。

When compared to the sprintf family, the C++ streams are supposed to be slower (by a factor 6 if I recall an item of Exceptional C++, by Herb Sutter). Still, most of the time, you won't need this speed, but you need to be sure your code won't be bugged.

这很容易做printf系列函数出现错误,无论是输入错误的参数数量,错误的类型,甚至在代码中引入潜在的安全漏洞(%n说明符)。

And it is easy to do something wrong with the printf family of functions, be it putting the wrong number of arguments, the wrong types, or even introduce potential security vulnerability (the %n specifier comes to mind) in your code.

除非真的想要它(然后,它被称为破坏),这是几乎不可能的错误与C ++流。它们无缝地处理所有已知类型(内置,std ::字符串等),它很容易扩展它。例如,假设我有一个对象Coordinate3D,并且我要打印它的数据:

Unless really wanting it (and then, it's called sabotage), it's almost impossible to get it wrong with C++ streams. They handle seamlessly all known types (build-ins, std::strings, etc.), and it's easy to extend it. For example, let's say I have an object "Coordinate3D", and that I want to print out its data:

#include <iostream>

struct Coordinate3D
{
    int x ;
    int y ;
    int z ;
} ;

std::ostream & operator << (std::ostream & p_stream
                          , const Coordinate3D & p_c)
{
    return p_stream << "{ x : " << p_c.x
                   << " , y : " << p_c.y
                   << " , z : " << p_c.z << " }" ;
}

int main(int argc, char * argv[])
{
    Coordinate3D A = {25,42,77} ;
    std::cout << A << std::endl ;
          // will print "{ x : 25 , y : 42 , z : 77 }"
    return 0 ;
}

流的问题是它们很难正确处理以指定一些数据的格式(例如,用于数字的填充空格),有时,真的真的需要快速。然后,回退到printf,或尝试一些高速C ++替代( FastFormat )。

The problem with the stream is that they are quite difficult to handle correctly when wanting to specify format of some data (padding spaces for numbers, for example), and that sometimes, you really really need to go fast. Then, either fall back to printf, or try some high-speed C++ alternatives (FastFormat comes to mind).

编辑:请注意, Thomas 系列测试显示了有趣的结果(我现在在我的电脑上复制),即: cout printf 有类似的性能,当避免使用 std :: endl 输出 \\\
)。

Note that Thomas' series of tests show interesting results (which I reproduced right now on my computer), that is: cout and printf have similar performances when one avoids using std::endl (which flushes the output in addition to outputing a \n).

这篇关于这更快,而且更灵活:printf或cout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆