Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > C language paradox

Reply
Thread Tools

C language paradox

 
 
pembed2012
Guest
Posts: n/a
 
      02-28-2012
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?
 
Reply With Quote
 
 
 
 
Willem
Guest
Posts: n/a
 
      02-28-2012
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?

Have you compared the output of the two programs to see if it is the same?


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      02-28-2012
On 02/29/12 10:33 AM, 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");


This outputs one newline. It will probably be optimised to a putchar().

> time: 0.38 seconds
>
> routine: for(i = 0;i< 100: i++) puts("\n");


This outputs two newlines.

>
> Conclusion: printf is more efficient than puts.


Conclusion: outputting two characters takes longer than outputting one.

--
Ian Collins
 
Reply With Quote
 
Shao Miller
Guest
Posts: n/a
 
      02-28-2012
On 2/28/2012 16:33, pembed2012 wrote:
> Hello again
> I continue to read about optimization in C.


And you continue to keep your sources private. "I have heard that
malloc also allocate additional memory to hold some header information."

>
> I saw that we shall always uses puts instead of printf for a constant
> string.


Where did you see that?

> 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.
>


If you believe that you can generalize and conclude that this test's
result applies across all C implementations everywhere, I don't think
that's a good belief.

> Can anyone explain me this paradox?


What paradox? I heard that you were just trying to make the regulars on
comp.lang.c do a dance, as seems to be fairly common. But I doubt you
will respond to this post. Can you explain that paradox to me?

How about betting your friend $5.00 that you can get 10 people to tell
you that the 'puts' above outputs two characters and the 'printf' just
one. You win!
 
Reply With Quote
 
Kaz Kylheku
Guest
Posts: n/a
 
      02-28-2012
On 2012-02-28, pembed2012 <(E-Mail Removed)> wrote:
> Hello again
> I continue to read about optimization in C.
>
> I saw that we ...

[...]
> Conclusion ...

[...]
> Can anyone explain me this paradox?


1. Whatever you read is false, and your conclusion from one experiment is also
a false generalization.

2. A difference between what you "saw" (whatever that means,
probably "read on some silly web page") and what you observed in an
experiment is not a "paradox". A paradox is an apparent contradiction which
is actually true.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      02-28-2012
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.
 
Reply With Quote
 
James Dow Allen
Guest
Posts: n/a
 
      02-28-2012
On Feb 29, 4:33*am, pembed2012 <(E-Mail Removed)> 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.


I find such substitution for 'printf' INTENSELY annoying.
It's not at all unusual that I search for every instance
of printf in a file. To miss some where the "clever"
programmer had "optimized" to puts() or putc() would
infuriate me, perhaps even enough for
rm -rf idiots_source

BTW, if you're in the "optimization is fun even where
unneeded" camp, I'm one of the few in this ng who's on your side.
Writing The_Worlds_Fastest_Jpeg(tm) was great fun for me,
and involved some "clever" tricks worth much less than 1%
speedup each. But these were optimizations intended to
.... er ... optimize!, not inane trivia like bypassing
printf.

James
 
Reply With Quote
 
Stephen Sprunk
Guest
Posts: n/a
 
      02-29-2012
On 28-Feb-12 16:38, James Kuyper wrote:
> 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.


.... unless the compiler is smart enough to recognize that case and
substitute a call to puts() instead. GCC, for instance, has been doing
that for ages.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-29-2012
pembed2012 <(E-Mail Removed)> writes:
> 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?


Get a faster computer. It shouldn't take that long to write 100 or
200 empty lines. (Unless you have a test harness that you haven't
shown us.)

I usually use puts() for constant strings, but only because it's
simpler than printf(); I don't have to worry about '%' characters in
the argument being interpreted, and it provides a newline character
(assuming I want one).

On the other hand, for a beginner using printf consistently is
probably simpler, since there's only one function to remember (which
may be why the classic "hello, world" uses printf rather than puts).

If the performance difference between equivalent puts and printf
calls is a significant concern for you, either you're doing
something strange or you're overly concerned about things that
really don't matter much. "Something strange" might include using
an old and/or tiny system where the code size for printf and its
internal interpreter is a significant burden.

(Oh, and the two snippets aren't equivalent, as everyone else has
already pointed out.)

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      02-29-2012
On 2/28/2012 4:33 PM, pembed2012 wrote:
> Hello again
> I continue to read about optimization in C.


Bad idea. If you're the same person who just two weeks ago (under
a different name but from the same address) posted a beginner's question
about `for' loops, you are doing yourself a disservice by wasting time
on irrelevancies. A graduate of Le Cordon Bleu may legitimately ponder
whether to add a dash of nutmeg to a recipe calling for mace, but the
same is not appropriate in someone who is still learning to separate
eggs.

Jackson's Laws of Program Optimization are

First Law: Don't do it.

Second Law (for experts only): Don't do it yet.

> 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.


Conclusion: At your current state of development, the Second Law
does not apply.

--
Eric Sosman
(E-Mail Removed)d
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Paradox Language driver change kabamarou C++ 1 05-11-2007 10:45 AM
Wireless Performance Paradox on Thinkpad T42p Sid Joyner Wireless Networking 0 09-06-2005 01:00 AM
Connecting with paradox file.. Ricardo Magalhaes ASP .Net 0 04-12-2005 07:49 PM
Is this a PARADOX? =?Utf-8?B?VVJHRU5ULi5QbGVhc2U=?= ASP .Net 3 09-05-2004 06:39 PM
comp.sys.mac.games.misc,comp.sys.ibm.pc.hardware.misc,comp.mail.pegasus-mail.ms-windows,comp.databases.paradox,comp.dcom.sys.cisco . Cisco 0 06-19-2004 07:48 AM



Advertisments