Valhalla Legends Forums Archive | C/C++ Programming | iostream vs stdio

AuthorMessageTime
Arta
Just wondered. I'm definitely a stdio man.
November 19, 2003, 8:58 PM
Eibro
I generally use streams, as they're easily extensible.
I use boost::format for anything that needs to be formatted.
November 19, 2003, 9:08 PM
iago
I use both, depending on the situation. But I try to stay consistant in a single project.

Overall, I use iostream more. I like to stay modern, stick with C++ syntax, especially if I'm using classes. I use new, iostream, fstream, etc.
November 19, 2003, 10:08 PM
Moonshine
Whilst stdio still has its uses (I especially like the ease of use with the va_list style function parameters), I'll have to go with streams; for they're more extensible, and type-safe.
November 19, 2003, 11:28 PM
Adron
stdio is my thing. With it, power is at my fingertips.

iostream is nice for when you have projects with lots of classes that you want to be able to stream in and out.
November 20, 2003, 12:53 AM
wut
stdio, because I'm much more apt to use C (although I usually compile my code with g++ anyways because I like C++ type checking)
November 20, 2003, 1:53 AM
Eibro
[quote author=Moonshine link=board=30;threadid=3712;start=0#msg30191 date=1069284537]
Whilst stdio still has its uses (I especially like the ease of use with the va_list style function parameters), I'll have to go with streams; for they're more extensible, and type-safe.
[/quote]

[quote]stdio, because I'm much more apt to use C (although I usually compile my code with g++ anyways because I like C++ type checking)[/quote]Honestly, if you two haven't already, i'd check out boost::format. Typesafe, printf-like, and uses a typesafe method for variable length argument lists. Best option i've seen for formatting.
November 20, 2003, 3:12 AM
Kp
[quote author=Eibro link=board=30;threadid=3712;start=0#msg30267 date=1069297958]Honestly, if you two haven't already, i'd check out boost::format. Typesafe, printf-like, and uses a typesafe method for variable length argument lists. Best option i've seen for formatting.[/quote]But does it compile into as good an assembly as doing printf? :) Also, there's the obvious issue that it won't work at all in C programs.
November 20, 2003, 3:38 AM
iago
[quote author=Kp link=board=30;threadid=3712;start=0#msg30271 date=1069299492]
[quote author=Eibro link=board=30;threadid=3712;start=0#msg30267 date=1069297958]Honestly, if you two haven't already, i'd check out boost::format. Typesafe, printf-like, and uses a typesafe method for variable length argument lists. Best option i've seen for formatting.[/quote]But does it compile into as good an assembly as doing printf? :) Also, there's the obvious issue that it won't work at all in C programs.
[/quote]

I have no idea how printf works once it does the call, to parse in the stuff, but I DO know how cin/cout parses the stuff. So for that reason, iostream!

I though kp would have more to say, I was waiting for his incisive, case-breaking argument! :(
November 20, 2003, 6:58 AM
Arta
As was I. I'd be interested in an explanation of the way printf compiles. I've often wondered how it is as fast as it is, with the amount of parsing/concatenation it must do.
November 20, 2003, 1:46 PM
Kp
[quote author=iago link=board=30;threadid=3712;start=0#msg30294 date=1069311483]
[quote author=Kp link=board=30;threadid=3712;start=0#msg30271 date=1069299492]But does it compile into as good an assembly as doing printf? :) Also, there's the obvious issue that it won't work at all in C programs.[/quote]

I have no idea how printf works once it does the call, to parse in the stuff, but I DO know how cin/cout parses the stuff. So for that reason, iostream![/quote]
If you've looked at the same asm that I have for how cin/cout work when you do a complex output (i.e. the following code sample), I'd be surprised to hear you say that. From g++ 3.3.1:

// asm output of cout << "String" << (int) 0 << "hmm" << *argv;
// passed call names back through demangler to make it readable
[code]movl $LC0, 4(%esp)
movl $std::cout, (%esp)
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
movl $0, 4(%esp)
movl %eax, (%esp)
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
movl $LC1, 4(%esp)
movl %eax, (%esp)
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
movl %eax, %edx
movl 12(%ebp), %eax
movl (%eax), %eax
movl %eax, 4(%esp)
movl %edx, (%esp)
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
[/code]

// asm output from printf ("String%dhmm%s", 0, *argv);
[code]movl 12(%ebp), %eax
movl (%eax), %eax
movl %eax, 8(%esp)
movl $0, 4(%esp)
movl $LC0, (%esp)
call _printf[/code]

It's also worth noting that doing [b]cout << "hmm" << "beh"
will result in two calls, but that printf ("hmm" "beh"); will not because the compiler can concatenate hmm and beh into a single string, which is then passed to printf.

[quote author=iago link=board=30;threadid=3712;start=0#msg30294 date=1069311483]I though kp would have more to say, I was waiting for his incisive, case-breaking argument! :(
[/quote]
I was in a hurry.

[quote author=Arta[vL] link=board=30;threadid=3712;start=0#msg30310 date=1069336001]
As was I. I'd be interested in an explanation of the way printf compiles. I've often wondered how it is as fast as it is, with the amount of parsing/concatenation it must do.
[/quote]
If you mean how the caller compiles, it's shown above. I'd have to go find a copy of printf source to show you how the function itself is compiled. My recollection is that it works so quickly because it can directly output any character which isn't a % sign, then go into a switch if it receives a %. That switch handles the various cases (s, c, i, d, f, etc.) in different ways, then reverts to copying non-% data directly to output. A further optimization would be to simply scan forward til a % (or nul) is found, output everything seen so far, then handle the character that stopped the search. Repeat as necessary.

[Edit: moved the commentaries that label the code. Quote tags don't like bold, apparently. Also fixed issue where subscripting argv confuses the forum. There needs to be some symbol other than brackets to indicate markups.]
November 20, 2003, 3:28 PM
iago
A function call isn't awfully slow, though, but I wonder how much slower iostream actually is?

Also note that in modern computers the speed of displaying text to the screen is hardly an issue.

And you still haven't covered the part about how iostream is expandable :P
November 20, 2003, 6:25 PM
Adron
[quote author=Kp link=board=30;threadid=3712;start=0#msg30315 date=1069342107]
It's also worth noting that doing cout << "hmm" << "beh" will result in two calls, but that printf ("hmm" "beh"); will not because the compiler can concatenate hmm and beh into a single string, which is then passed to printf.

[/quote]

What about cout << "hmm" "beh" ?
November 20, 2003, 7:11 PM
Kp
[quote author=iago link=board=30;threadid=3712;start=0#msg30325 date=1069352724]
A function call isn't awfully slow, though, but I wonder how much slower iostream actually is?[/quote]

No, but look at how much space the iostream call sequence took up vs. how much space the printf call took.

[quote author=iago link=board=30;threadid=3712;start=0#msg30325 date=1069352724]And you still haven't covered the part about how iostream is expandable :P[/quote]

Why would I? :) I rarely deal with programs where it's appropriate for objects to go printing themselves.

[quote author=Adron link=board=30;threadid=3712;start=0#msg30338 date=1069355481]What about cout << "hmm" "beh" ?[/quote]

That should get concatenated by the compiler too.
November 20, 2003, 10:44 PM
iago
[quote author=Kp link=board=30;threadid=3712;start=0#msg30375 date=1069368289]
[quote author=iago link=board=30;threadid=3712;start=0#msg30325 date=1069352724]
A function call isn't awfully slow, though, but I wonder how much slower iostream actually is?[/quote]

No, but look at how much space the iostream call sequence took up vs. how much space the printf call took.
[/quote]

So just becuase it makes longer assembly, it's worse? That's hardly an argument, especially if each of those calls go to a short function and a single printf call goes to a large complicated function.
November 20, 2003, 11:07 PM
Adron
If those large calls go to short functions and that small call goes to a long function where the entire code isn't executed, the small call seems mostly better to me. It depends on how often you'll be calling it though...
November 21, 2003, 12:49 AM
Arta
I think Iago makes a good point. On the face of it, it looks as though stdio is clearly superior, but i very much doubt it's as clear-cut as that. I was referring more to the internals of printf and cout.
November 21, 2003, 3:12 AM
iago
Write a program that counts from 0 to INT_MAX or 1000000 or something, and have it print out each number (doesn't matter which way). Time it. When it finishes, do it again without printing. Notice how it takes like 1% of the time (I'm guessing, never actually tried)? I/O is extremely slow, so I don't see why it matters whether or not the code you're using is terribly efficient for calling the I/O in the first place!

Even stupider is worrying about how well cin works, but I don't need to explain that one :P
November 21, 2003, 4:06 AM
Kp
[quote author=iago link=board=30;threadid=3712;start=15#msg30476 date=1069387619]
I/O is extremely slow, so I don't see why it matters whether or not the code you're using is terribly efficient for calling the I/O in the first place![/quote]

Sure, it's slow. However, as I recall, it's faster to write output to something other than the terminal due to better buffering. Even setting that aside, it seems like a bad idea to justify non-optimization of code on the grounds that it will still be slow. Java programs run slowly because they're (usually) bytecode, and that's all the more reason that they need to be optimized, so that the slowness of the VM is the only thing pestering the user.
November 21, 2003, 5:02 AM
Arta
I agree with kp :)

I might run that test when I'm bored one day though, it could be interesting.
November 21, 2003, 4:47 PM
St0rm.iD
On a performance-unrelated note, I think that C++ has too much operator overloading nonsense. I think operator overloading is good for stuff like ==, >, < etc, but it can get confusing when an operator does something it doesn't usually do.

For example, when I first learned C++, it took me forever to figure out that I wasn't bitshifting a string to write it to the console.
November 22, 2003, 1:09 AM
Hostile
Personally I object to STD Input/Output. Its a terrible thing which should be avoided. :P
November 22, 2003, 1:21 AM
Adron
[quote author=iago link=board=30;threadid=3712;start=15#msg30476 date=1069387619]
I/O is extremely slow, so I don't see why it matters whether or not the code you're using is terribly efficient for calling the I/O in the first place!
[/quote]

This means that the speed of the code doesn't matter. The size still does though if you have a lot of printing in your application. Smaller executable!

November 22, 2003, 2:52 AM
Adron
[quote author=Hostile link=board=30;threadid=3712;start=15#msg30607 date=1069464086]
Personally I object to STD Input/Output. Its a terrible thing which should be avoided. :P
[/quote]

Standard input/output make it easy for users to do big things by combining several small tools through piping etc.
November 22, 2003, 2:54 AM
Hostile
[quote author=Adron link=board=30;threadid=3712;start=15#msg30622 date=1069469672]
[quote author=Hostile link=board=30;threadid=3712;start=15#msg30607 date=1069464086]
Personally I object to STD Input/Output. Its a terrible thing which should be avoided. :P
[/quote]

Standard input/output make it easy for users to do big things by combining several small tools through piping etc.
[/quote]

hmm, I had a slightly hidden joke in there, perhaps misplaced. I was refering to STDs (Sexually Transmitted Diseases).
November 23, 2003, 5:16 AM
Adron
[quote author=Hostile link=board=30;threadid=3712;start=15#msg30844 date=1069564571]
hmm, I had a slightly hidden joke in there, perhaps misplaced. I was refering to STDs (Sexually Transmitted Diseases).
[/quote]

Oh, and after all the STD arguing I've done, I missed that. What a shame. I was thinking STD_INPUT_HANDLE...
November 23, 2003, 11:30 AM

Search