本文介绍了puts()vs printf()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我最近在我的一本旧C书中读过,对于性能而言,put()是一个更好的

函数调用,而不是
$ b $中的printf() b以下情况:


put(一些随机文本);


vs.


printf(一些随机文本\ n);


[阅读:无格式化。]

有人请确认或否认这个吗?这是有意义的,因为

printf()使用vargs,但我想要一些确认。作者使用

void main()所以我不知道该怎么想。鉴于今天的硬件和

编译器优化,我不知道它是否重要。我不是很擅长现代装配,所以我不能这样比较。

我一般都不会放弃看跌期权 -

过去的I / O功能。我不知道为什么会这样......也许它与

的一致性有关,因为我并不总是希望换行自动输出。


这是什么字?


谢谢。


ps这本书是由Herb Schlidt(或类似的东西)写的,如果有任何

指示。

I''ve recently read in one of my old C books that puts() is a better
function call with regard to performance than printf() in the
following situation:

puts("Some random text");

vs.

printf("Some random text\n");

[Read: no formatting.]
Would anyone please confirm or deny this? It makes sense since
printf() uses vargs, but I''d like some confirmation. The author uses
void main() so I''m not sure what to think. Given today''s hardware and
compiler optimizations I have no idea if it even matters. I''m not very
good at modern-day assembly, so I can''t compare it that way.
I''ve generally steered away from the puts-gets I/O functions in the
past. I don''t know why this is the case...maybe it has to do with
consistency as I don''t always want the newline automatically output.

What''s the word?

Thanks.

p.s. The book is by Herb Schlidt (or something similar) if that''s any
indication.

推荐答案



printf()-likely-无条件地调用va_init(),但在那之后

没有参数可以处理,因为没有格式

元素。调用或不调用va_init()将是一个恒定的时间

因子,可能几乎检测不到。


printf()必须至少检查一次每个字符的顺序

弄清楚它不是格式字符。如果它使用

putc()来输出字符,那么理论上它可以立即输出每个字符的
。它需要测试每个角色

无论如何它都是\ 0,但是每个角色两次比较

而不是一个可能在足够长的字符串上可以检测到的。


如果printf()通过首先将整个字符串打印到缓冲区

来实现,例如通过调用sprintf()然后输出结果

字符串,那么这样做的开销可能是可以检测到的。


如果你考虑一下,你会看到有效的

没有printf()做的工作方式比puts()做的更好:如果有

那么puts()就会使用该方法。那么问题

成为QOI(实施质量)之一,以及

关于指令集架构的问题。从理论上讲,每个循环的两个
测试不一定比任何一个慢*,因为

可能是一个有效的指令移动字符串直到

delimeter",或者两个测试可能重叠,或紧密的

循环可以进行微优化。 (指令集可见

编程级别通常不是使用的指令集

内部:微编码很常见。)

-

如果你撒谎到编译器,它将报复。 - Henry Spencer

printf() -likely- calls va_init() unconditionally, but after that
there would be no arguments to process as there are no format
elements. Calling or not calling va_init() would be a constant time
factor, probably nearly undetectable.

printf() must examine each character at least once in order to
figure out that it is -not- a format character. If it is using
putc() to output characters, then in theory it could just output
each character immediately. It needs to test each character to
see if it is \0 anyhow, but two comparisons per character
instead of one might detectable on long enough strings.

If printf() is implimented by printing the entire string into a buffer
first, such as by calling sprintf() and then outputing the resulting
string, then the overhead of doing that would likely be detectable.

If you think about it, you will see that there is effectively
no way for printf() to do -less- work than puts() does: if there
were then puts() would use that method instead. The questions then
becomes one of QOI (Quality of Implementation), together with
a question about instruction set architecture. In theory, two
tests per loop is not *necessarily* any slower than one, as there
could be an instruction that was effectively "move string until
delimeter", or the two tests might be overlapped, or the tight
loop could be micro-optimized. (The instruction set visible to
the programming level is often not the instruction set used
internally: microcoding is quite common.)
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer




puts(str)通常比printf(str)快,因为它

不需要搜索`str''转换规格

喜欢%d。 (请注意,如果`str''包含任何看起来像某个转换规范的内容,那么printf(str)将会非常麻烦 - printf(") category \tcount\t%frequency \ n")

绝对是禁忌。)


也就是说,差异很可能如此小,以至于它不会引起注意,甚至可能很小,以至于难以衡量。考虑一下:你有实际的数据输出操作

正在进行中,以便将printf()/ puts()字符传递给他们的目的地--b
磁盘文件,管道,电缆调制解调器与WhizFiber,

或其他什么。所有这些通道都比执行函数调用的

CPU慢得多;通过切换到更多

效率各种各样的你可能只是在增加闲置循环的迭代次数。


我的一位朋友曾经将这种优化描述为

清理瓶子顶部的海滩,这样沙子就会很漂亮

并清理掉搁浅的鲸鱼尸体。考虑一下。

puts(str) is usually faster than printf(str) because it
doesn''t need to search through `str'' for conversion specifications
like "%d". (Note that if `str'' contains anything that looks
even vaguely like a conversion specification, printf(str) will
be in deep trouble -- printf("category\tcount\t%frequency\n")
is most definitely a no-no.)

That said, the difference is likely to be so small that it''s
unnoticeable, probably even so small that it will be difficult
to measure. Consider: You''ve got actual data output operations
going on, in order to deliver the printf()/puts() characters to
their destination -- disk file, pipe, cable modem with WhizFiber,
or whatever. All of these channels are VASTLY slower than the
CPU that executes the function call; by switching to the "more
efficient" variety you may be doing nothing more than increasing
the iteration count of the idle loop.

A friend of mine once described this sort of optimization as
"Cleaning the bottle tops off the beach so the sand will be nice
and clean around the beached whale carcases." Ponder that.



我个人来说,从未读过Herr Schildt的任何作品。

其他人有,并且更加明确地对它们发表了评论而不是

我可以;例如,请参阅


据观察,Schildt的The Annotated C Standard

的成本低于标准本身,并且已经认为

价格的差异反映了注释的价值。


-

Eric Sosman
lid

I, personally, have never read any of Herr Schildt''s works.
Others have, and have commented on them more perspicaciously than
I could; see, for example,

http://www.lysator.liu.se/c/schildt.html

It has been observed that Schildt''s "The Annotated C Standard"
costs less than the Standard itself, and it has been opined that
the difference in price reflects the value of the annotations.

--
Eric Sosman
es*****@acm-dot-org.invalid




这绝对是一个实施质量问题。我个人

相信这个说法一般都是正确的,但那个和四分之一仍然不会给你买一杯咖啡。

It is strictly a quality of implementation issue. I personally
believe the assertion is generally true but that and a quarter still
won''t buy you a cup of coffee.



不是因为变量参数而是因为puts是单一的

函数(将已经格式化的字符串发送到stdout) printf有很多铃声和口哨,它应该花更多的时间来计算

你真正想做的事情是一样的。


一些聪明的图书馆作家也有可能决定这个

对printf的退化使用是如此常见,以至于在代码的早期进行了特殊检查

,因此差异是无法估量的。

Not because of variable arguments but because puts is a single minded
function (send an already formatted string to stdout) while printf has
so many bells and whistles that it "should" take more time to figure
out what it is you really want to do is the same.

It is also possible that some clever library writer decided this
degenerative use of printf is so common that a special check is made
early in the code and therefore the difference is immeasurably small.



不幸的是很多书都有,但它仍然提醒作者提交作者很容易粗心。

A lot of books do unfortunately but it still serves as a reminder that
the author is prone to carelessness.



害怕,非常害怕!

删除del电子邮件

Be afraid, be very afraid!
Remove del for email


这篇关于puts()vs printf()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 03:09