Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: C language paradox |
| Date | 2012-02-28 17:38 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <4F4D576D.3000105@verizon.net> (permalink) |
| References | <jijh7o$i5c$1@speranza.aioe.org> |
On 02/28/2012 04:33 PM, pembed2012 wrote:
> Hello again
> I continue to read about optimization in C.
>
> I saw that we shall always uses puts instead of printf for a constant
> string. However I made the following experiments:
>
> routine: for(i = 0;i < 100; i++) printf("\n");
>
> time: 0.38 seconds
>
> routine: for(i = 0;i < 100: i++) puts("\n");
>
> time: 0.77 seconds
>
> Conclusion: printf is more efficient than puts.
>
> Can anyone explain me this paradox?
Well, it would be better to compare things which produce the same
output. If you're trying to produce 100 lines, replace the puts() call
with puts("") (or better yet, putchar('\n')). If you're trying to
produce 200 lines, change the printf() call to printf("\n\n").
In principle, printf() has to do a lot more work than puts(), even if
only to figure out that the format string it's been given doesn't
require it to do anything special. However, for a short simple format
string like this one, the difference should be quite minor compared to
the time spent writing the data to the output stream. You're more likely
to see a significant difference favoring puts() with a significantly
longer format string.
Also, keep in mind that a sufficiently clever implementation of C might
translate either of your loops by creating an array with 100 (or 200 :-)
characters, all but the last set to '\n', and the last one set '\0'. It
could then replace either of your loops with a single call to puts()
using that array. If you want to prevent such optimizations, C is not
the language for you.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
C language paradox pembed2012 <once@was.enough.invalid> - 2012-02-28 21:33 +0000
Re: C language paradox Willem <willem@toad.stack.nl> - 2012-02-28 21:41 +0000
Re: C language paradox Ian Collins <ian-news@hotmail.com> - 2012-02-29 10:44 +1300
Re: C language paradox Shao Miller <sha0.miller@gmail.com> - 2012-02-28 16:51 -0500
Re: C language paradox Kaz Kylheku <kaz@kylheku.com> - 2012-02-28 22:16 +0000
Re: C language paradox Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-02-29 01:34 -0800
Re: C language paradox James Kuyper <jameskuyper@verizon.net> - 2012-02-29 07:18 -0500
Re: C language paradox "Scott Fluhrer" <sfluhrer@ix.netcom.com> - 2012-02-29 13:39 -0500
Re: C language paradox Kenneth Brody <kenbrody@spamcop.net> - 2012-03-03 16:23 -0500
Re: C language paradox James Kuyper <jameskuyper@verizon.net> - 2012-03-03 19:47 -0500
Re: C language paradox Geoff <geoff@invalid.invalid> - 2012-03-03 18:05 -0800
Re: C language paradox Kenneth Brody <kenbrody@spamcop.net> - 2012-03-05 15:41 -0500
Re: C language paradox Robert Miles <milesrf@Usenet-News.net> - 2012-04-14 00:26 -0500
Re: C language paradox James Kuyper <jameskuyper@verizon.net> - 2012-02-28 17:38 -0500
Re: C language paradox Stephen Sprunk <stephen@sprunk.org> - 2012-02-28 18:52 -0600
Re: C language paradox James Kuyper <jameskuyper@verizon.net> - 2012-02-28 22:14 -0500
Re: C language paradox James Dow Allen <jdallen2000@yahoo.com> - 2012-02-28 15:08 -0800
Re: C language paradox "io_x" <a@b.c.invalid> - 2012-02-29 03:42 +0100
Re: C language paradox Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-02-29 01:37 -0800
Re: C language paradox jgharston <jgh@arcade.demon.co.uk> - 2012-03-03 16:17 -0800
Re: C language paradox nick_keighley_nospam@hotmail.com - 2012-03-04 02:54 -0800
Re: C language paradox "BartC" <bc@freeuk.com> - 2012-03-04 11:43 +0000
Re: C language paradox Keith Thompson <kst-u@mib.org> - 2012-03-04 04:01 -0800
Re: C language paradox Nomen Nescio <nobody@dizum.com> - 2012-03-04 14:34 +0100
Re: C language paradox Kaz Kylheku <kaz@kylheku.com> - 2012-03-04 17:01 +0000
Re: C language paradox Kenneth Brody <kenbrody@spamcop.net> - 2012-03-05 15:45 -0500
Re: C language paradox Keith Thompson <kst-u@mib.org> - 2012-02-28 17:45 -0800
Re: C language paradox army1987 <army1987@ask-for-it.invalid> - 2012-03-05 10:42 +0000
Re: C language paradox army1987 <army1987@ask-for-it.invalid> - 2012-03-05 10:48 +0000
Re: C language paradox Keith Thompson <kst-u@mib.org> - 2012-03-05 09:35 -0800
Re: C language paradox 88888 Dihedral <dihedral88888@googlemail.com> - 2012-04-16 13:01 -0700
Re: C language paradox China Blue Water Navy <chine.bleu@yahoo.com> - 2012-03-05 04:24 -0800
Re: C language paradox Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-03-05 07:49 -0500
Re: C language paradox "io_x" <a@b.c.invalid> - 2012-02-29 03:42 +0100
Re: C language paradox Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-02-28 21:46 -0500
csiph-web