Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > cin/cout vs. scanf/printf

Reply
Thread Tools

cin/cout vs. scanf/printf

 
 
Podrzut_z_Laweczki
Guest
Posts: n/a
 
      08-03-2006
Hello, I have question (or 2 ). Is that true that for a large data
using scanf/printf instead of cin/cout makes that the program runs
faster? And if it is, is it legal to mix scanf/printf with C++ code?
Program should execute below 1 sec and the hint is to use scanf/printf.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-03-2006
Podrzut_z_Laweczki wrote:
> Hello, I have question (or 2 ). Is that true that for a large data
> using scanf/printf instead of cin/cout makes that the program runs
> faster?


All generalizations are false.

> And if it is, is it legal to mix scanf/printf with C++ code?


Yes, it is legal. scanf/printf are parts of C++.

> Program should execute below 1 sec and the hint is to use
> scanf/printf.


The hint to whom?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      08-03-2006
Podrzut_z_Laweczki wrote:
> Hello, I have question (or 2 ). Is that true that for a large data
> using scanf/printf instead of cin/cout makes that the program runs
> faster?


Sometimes, but the C-style I/O functions are also not typesafe, so your
development time may also increase. Try:

char c = 'a';
cout << c;
printf( "%s", c );

The third line sports undefined behavior, while the second line always
gets it right.

> And if it is, is it legal to mix scanf/printf with C++ code?


Look into ios_base::sync_with_stdio() (e.g., at
http://www.cplusplus.com/ref/iostrea...th_stdio.html).

> Program should execute below 1 sec and the hint is to use scanf/printf.


It depends on your compiler and optimizer, your computer, your data
source, your programming skill, etc. etc. etc., not just C vs. C++
style I/O.

Cheers! --M

 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      08-03-2006
On 3 Aug 2006 12:54:00 -0700, "Podrzut_z_Laweczki" <(E-Mail Removed)>
wrote in comp.lang.c++:

> Hello, I have question (or 2 ). Is that true that for a large data
> using scanf/printf instead of cin/cout makes that the program runs
> faster?


Yes, it's true. No, it's not true. Neither the C nor C++ standard
specify the absolutely or relative speed of any operator or function.
The C++ standard does specify the order of some operations, but order
alone does not define speed.

> And if it is, is it legal to mix scanf/printf with C++ code?


It is legal to use <cstdio> functions in a C++ program is you use them
correctly. It is rarely a good idea to use scanf() in either C or
C++, as it is very difficult and complex to use correctly.

> Program should execute below 1 sec and the hint is to use scanf/printf.


What program? Who's hint? How long does the program take when you
use the cin and cout streams?

First write a program that runs correctly and meets all its
requirements. Then test it thoroughly, especially for corner cases.
Then, and only then, if it runs correctly but too slowly, you can
start thinking about ways to make it faster.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      08-03-2006
mlimber <(E-Mail Removed)> wrote:
> Sometimes, but the C-style I/O functions are also not typesafe, so your
> development time may also increase. Try:
>
> char c = 'a';
> cout << c;
> printf( "%s", c );
>
> The third line sports undefined behavior, while the second line always
> gets it right.


Almost, except when you have a pointer to char, at which point it tries
to treat it as a C-style string. For example:


#include <iostream>

int main()
{
int i = 42;
std::cout << "&i = " << &i << '\n';

char c = 'a';
std::cout << "&c = " << &c << '\n';
}


Output I got:

&i = 0012FEE4
&c = a*


In order to work around this, one method is to perform a cast:

std::cout << "&c = " << static_cast<void*>(&c) << '\n';

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      08-03-2006

Podrzut_z_Laweczki wrote:
> Hello, I have question (or 2 ). Is that true that for a large data
> using scanf/printf instead of cin/cout makes that the program runs
> faster? And if it is, is it legal to mix scanf/printf with C++ code?
> Program should execute below 1 sec and the hint is to use scanf/printf.


I tested this once, I think, and the result was negative...c++ streams
where faster. At any rate, it is usually rather insignificant in the
larger scheme...string and stream operations are not usually your
bottlenecks.

 
Reply With Quote
 
Sjouke Burry
Guest
Posts: n/a
 
      08-03-2006
mlimber wrote:
> Podrzut_z_Laweczki wrote:
>
>>Hello, I have question (or 2 ). Is that true that for a large data
>>using scanf/printf instead of cin/cout makes that the program runs
>>faster?

>
>
> Sometimes, but the C-style I/O functions are also not typesafe, so your
> development time may also increase. Try:
>
> char c = 'a';
> cout << c;
> printf( "%s", c );
>
> The third line sports undefined behavior, while the second line always
> gets it right.


Did you make the stupid error in the printf line
on purpose or dont you know any better?
Any body used to using printf and its cousins,
knows that it is stupid to specify a char instead
of a string,and I am sure you can make just as
many stupid errors in the cout line.The cout
line might do the correct thing with an input
error,but it is stil an error.
If you want to prove a point, dont use false
arguments.
 
Reply With Quote
 
Markus Moll
Guest
Posts: n/a
 
      08-03-2006
Hi

Podrzut_z_Laweczki wrote:

> Hello, I have question (or 2 ). Is that true that for a large data
> using scanf/printf instead of cin/cout makes that the program runs
> faster?


Could be faster. Could as well be slower.

> Program should execute below 1 sec and the hint is to use scanf/printf.


Wild guess: you're doing some contest problems with huge inputs. Use
scanf/printf.

Markus

 
Reply With Quote
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      08-03-2006
Sjouke Burry schrieb:
> mlimber wrote:
>> Sometimes, but the C-style I/O functions are also not typesafe, so your
>> development time may also increase. Try:
>>
>> char c = 'a';
>> cout << c;
>> printf( "%s", c );
>>
>> The third line sports undefined behavior, while the second line always
>> gets it right.

>
> Did you make the stupid error in the printf line
> on purpose or dont you know any better?
> Any body used to using printf and its cousins,
> knows that it is stupid to specify a char instead
> of a string,and I am sure you can make just as
> many stupid errors in the cout line.The cout
> line might do the correct thing with an input
> error,but it is stil an error.
> If you want to prove a point, dont use false
> arguments.


Another arguments from another person:

1)
<beginner's mode>
size_t mysize = some_container.size();

printf("%u", mysize);

size_t is unsigned int, isn't it? Or what is the correct modifier for
size_t?
</>

2)
some_typedef myvar;
printf("???", myvar);

How to print unknown types, or types that may change/be redefined?

3)
myclass myobject;
printf("???", myobject);

How to print custom classes?


With stream, the compiler handles it for you, and you can implement
operator<< for custom types.

--
Thomas
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-03-2006
Sjouke Burry wrote:
> mlimber wrote:
>> Podrzut_z_Laweczki wrote:
>>
>>> Hello, I have question (or 2 ). Is that true that for a large data
>>> using scanf/printf instead of cin/cout makes that the program runs
>>> faster?

>>
>>
>> Sometimes, but the C-style I/O functions are also not typesafe, so
>> your development time may also increase. Try:
>>
>> char c = 'a';
>> cout << c;
>> printf( "%s", c );
>>
>> The third line sports undefined behavior, while the second line
>> always gets it right.

>
> Did you make the stupid error in the printf line
> on purpose or dont you know any better?
> Any body used to using printf and its cousins,
> knows that it is stupid to specify a char instead
> of a string,and I am sure you can make just as
> many stupid errors in the cout line.The cout
> line might do the correct thing with an input
> error,but it is stil an error.
> If you want to prove a point, dont use false
> arguments.


I suggest checking in your attitude at the door. The is no "false argument"
here. The simple demostration is supposed to show that 'printf' has no type
safety. And that's true. Besides, if you want to output an object, printf
is of no help.

Now, about the errors in the "cout line". *I* am sure that *you* can make
just as many stupid mistakes, but I would doubt very much that 'mlimber'
can (even if he tried). Now if you didn't intend to insult 'mlimber', then
you could probalby show us what kind of "stupid mistake" one can make on
a "cout line"... I am sure everybody would appreciate it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
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




Advertisments