cin/cout vs. scanf/printf

Discussion in 'C++' started by Podrzut_z_Laweczki, Aug 3, 2006.

  1. 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.
    Podrzut_z_Laweczki, Aug 3, 2006
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Aug 3, 2006
    #2
    1. Advertising

  3. Podrzut_z_Laweczki

    mlimber Guest

    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/iostream/ios_base/sync_with_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
    mlimber, Aug 3, 2006
    #3
  4. Podrzut_z_Laweczki

    Jack Klein Guest

    On 3 Aug 2006 12:54:00 -0700, "Podrzut_z_Laweczki" <>
    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/~ajo/docs/FAQ-acllc.html
    Jack Klein, Aug 3, 2006
    #4
  5. Podrzut_z_Laweczki

    Marcus Kwok Guest

    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.


    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
    Marcus Kwok, Aug 3, 2006
    #5
  6. Podrzut_z_Laweczki

    Noah Roberts Guest

    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.
    Noah Roberts, Aug 3, 2006
    #6
  7. Podrzut_z_Laweczki

    Sjouke Burry Guest

    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.
    Sjouke Burry, Aug 3, 2006
    #7
  8. Podrzut_z_Laweczki

    Markus Moll Guest

    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
    Markus Moll, Aug 3, 2006
    #8
  9. 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
    Thomas J. Gritzan, Aug 3, 2006
    #9
  10. 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
    Victor Bazarov, Aug 3, 2006
    #10
  11. Jack Klein wrote:
    > What program? Who's hint? How long does the program take when you
    > use the cin and cout streams?


    First of all thank you all for replying. It's nothing big, just a
    starter for online problems solving (Fibonacci numbers). Sry for this
    -> people send source files to a server (files are compiled by an
    automate) and get reply with some stats - whether it passed time limit,
    memory limit, if there were runtime or compilation errors etc.
    On the server it is compiled with GNU C++ 4.0.2, hint about
    printf/scanf is in faq. The task is here:
    http://opss.safo.biz/?menu=comp&sub=prob&comp=0&prob=1000
    I made sth like this (more or less the same as the spoiler). Are there
    any free progs for Windows and Linux to measure this speed and memory
    limit?

    #include <cstdio>
    int main()
    {
    double t[46] = {1,1};
    for (int i = 2; i < 46; ++i)
    t = t[i-1] + t[i-2];
    int d;
    scanf("%d",&d);
    if ((d>=1)&&(d<=1000)){
    while (d--){
    int j;
    scanf("%d",&j);
    printf("%.0lf\n",t[j]);
    }
    }
    }
    Podrzut_z_Laweczki, Aug 3, 2006
    #11
  12. Podrzut_z_Laweczki

    Default User Guest

    Thomas J. Gritzan wrote:


    > <beginner's mode>
    > size_t mysize = some_container.size();
    >
    > printf("%u", mysize);
    >
    > size_t is unsigned int, isn't it?


    No, it is an unsigned integral type. Usually unsigned long.

    > Or what is the correct modifier for
    > size_t?


    There isn't one in C++. The new C standard added one. The usual
    practice is to use ld and cast the value to unsigned long.




    Brian
    Default User, Aug 3, 2006
    #12
  13. Default User wrote:
    > Thomas J. Gritzan wrote:
    >
    >
    >> <beginner's mode>
    >> size_t mysize = some_container.size();
    >>
    >> printf("%u", mysize);
    >>
    >> size_t is unsigned int, isn't it?

    >
    > No, it is an unsigned integral type. Usually unsigned long.
    >
    >> Or what is the correct modifier for
    >> size_t?

    >
    > There isn't one in C++. The new C standard added one. The usual
    > practice is to use ld and cast the value to unsigned long.


    Since the size of 'size_t' (which is its own type, not a typedef for
    anything) is implementation-defined, you should consider looking for
    an implemenation-specific solution. For example, in Win64 casting
    to unsigned long is not going to work very well. Microsoft did add
    the I64 specifier (IIRC) for printing out types that are longer than
    unsinged long.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 4, 2006
    #13
  14. On Thu, 03 Aug 2006 15:41:07 -0700, Podrzut_z_Laweczki wrote:

    >
    > Jack Klein wrote:
    >> What program? Who's hint? How long does the program take when you
    >> use the cin and cout streams?

    >
    > First of all thank you all for replying. It's nothing big, just a
    > starter for online problems solving (Fibonacci numbers). Sry for this
    > -> people send source files to a server (files are compiled by an
    > automate) and get reply with some stats - whether it passed time limit,
    > memory limit, if there were runtime or compilation errors etc.
    > On the server it is compiled with GNU C++ 4.0.2, hint about
    > printf/scanf is in faq. The task is here:
    > http://opss.safo.biz/?menu=comp&sub=prob&comp=0&prob=1000
    > I made sth like this (more or less the same as the spoiler). Are there
    > any free progs for Windows and Linux to measure this speed and memory
    > limit?
    >
    > #include <cstdio>
    > int main()
    > {
    > double t[46] = {1,1};
    > for (int i = 2; i < 46; ++i)
    > t = t[i-1] + t[i-2];
    > int d;
    > scanf("%d",&d);
    > if ((d>=1)&&(d<=1000)){
    > while (d--){
    > int j;
    > scanf("%d",&j);
    > printf("%.0lf\n",t[j]);
    > }
    > }
    > }



    return 0;

    at the end of main, to keep the compiler happy.

    To see if it passes the 1 second time limit, try it yourself.
    Build an input file with 1000 datasets, and time it.
    On a unix system (probably windows too), you can use the
    rand() or random() function for that.

    Incidentally, except for cstdio, your code is all C.
    Amadeus W. M., Aug 4, 2006
    #14
  15. Amadeus W. M. wrote:
    > On Thu, 03 Aug 2006 15:41:07 -0700, Podrzut_z_Laweczki wrote:
    >
    >>
    >> Jack Klein wrote:
    >>> What program? Who's hint? How long does the program take when you
    >>> use the cin and cout streams?

    >>
    >> First of all thank you all for replying. It's nothing big, just a
    >> starter for online problems solving (Fibonacci numbers). Sry for this
    >> -> people send source files to a server (files are compiled by an
    >> automate) and get reply with some stats - whether it passed time
    >> limit, memory limit, if there were runtime or compilation errors etc.
    >> On the server it is compiled with GNU C++ 4.0.2, hint about
    >> printf/scanf is in faq. The task is here:
    >> http://opss.safo.biz/?menu=comp&sub=prob&comp=0&prob=1000
    >> I made sth like this (more or less the same as the spoiler). Are
    >> there any free progs for Windows and Linux to measure this speed and
    >> memory limit?
    >>
    >> #include <cstdio>
    >> int main()
    >> {
    >> double t[46] = {1,1};
    >> for (int i = 2; i < 46; ++i)
    >> t = t[i-1] + t[i-2];
    >> int d;
    >> scanf("%d",&d);
    >> if ((d>=1)&&(d<=1000)){
    >> while (d--){
    >> int j;
    >> scanf("%d",&j);
    >> printf("%.0lf\n",t[j]);
    >> }
    >> }
    >> }

    >
    >
    > return 0;
    >
    > at the end of main, to keep the compiler happy.


    If your compiler is unhappy about the absence of 'return 0;',
    throw away that compiler and get yourself a happier one.

    > To see if it passes the 1 second time limit, try it yourself.
    > Build an input file with 1000 datasets, and time it.
    > On a unix system (probably windows too), you can use the
    > rand() or random() function for that.
    >
    > Incidentally, except for cstdio, your code is all C.


    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 4, 2006
    #15
  16. Amadeus W. M. wrote:
    > [...]
    > int main()
    > {
    > [...]
    > }
    >
    > return 0;
    >
    > at the end of main, to keep the compiler happy.


    If your C++ compiler needs that, throw it away, it's not fully
    compliant.

    > To see if it passes the 1 second time limit, try it yourself.
    > Build an input file with 1000 datasets, and time it.
    > On a unix system (probably windows too), you can use the
    > rand() or random() function for that.
    >
    > Incidentally, except for cstdio, your code is all C.


    Avtually, that's not true. C requres 'int main(void)'. The 'void'
    is necessary.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 4, 2006
    #16
  17. Amadeus W. M. wrote:
    > To see if it passes the 1 second time limit, try it yourself.
    > Build an input file with 1000 datasets, and time it.
    > On a unix system (probably windows too), you can use the
    > rand() or random() function for that.


    So there are no free programs to measure that? Or plugins or modules or
    whatever that does it - freezes and shows how much time and memory does
    it take? As I understand rand() would be only for creating these sets,
    so I still don't get it how to "time it" - with a hand watch, or am I
    writing nonsense?. Sry to bother that much.
    Podrzut_z_Laweczki, Aug 4, 2006
    #17
  18. Podrzut_z_Laweczki <> wrote:

    > [..] Are there
    > any free progs for Windows and Linux to measure this speed and memory
    > limit?


    There is a 'clock' function and a 'time' function which you can use
    at the beginning and end of your main() function to time its execution
    time. 'clock' probably has a higher resultion than 'time', tho.

    hth
    --
    jb

    (reply address in rot13, unscramble first)
    Jakob Bieling, Aug 4, 2006
    #18
  19. On Thu, 03 Aug 2006 21:14:58 -0700, Podrzut_z_Laweczki wrote:

    >
    > Amadeus W. M. wrote:
    >> To see if it passes the 1 second time limit, try it yourself.
    >> Build an input file with 1000 datasets, and time it.
    >> On a unix system (probably windows too), you can use the
    >> rand() or random() function for that.

    >
    > So there are no free programs to measure that? Or plugins or modules or
    > whatever that does it - freezes and shows how much time and memory does
    > it take? As I understand rand() would be only for creating these sets,
    > so I still don't get it how to "time it" - with a hand watch, or am I
    > writing nonsense?. Sry to bother that much.



    Oh, you time it with the "time" command. Like so:

    time your_progam < dataset

    at a command prompt, on any unix machine. It will be a few
    milliseconds. The more precise way is to use gettimeofday
    (man gettimeofday), from within your program.

    The cpu and memory usage (as well as time too),
    you can view with the "top" command, but because your program
    will finish so quickly, top won't catch it. If you want to get
    serious about it, try valgrind. That's the mother of all
    memory profiling tools. Will save you a lot of headaches in the
    future.

    You're worrying too much about a 5-line program.
    Amadeus W. M., Aug 4, 2006
    #19
  20. Podrzut_z_Laweczki

    Noah Roberts Guest

    Victor Bazarov wrote:

    > Avtually, that's not true. C requres 'int main(void)'. The 'void'
    > is necessary.


    Not according to the FAQ.

    http://c-faq.com/ansi/maindecl.html

    I know why you believe it is, and maybe the faq is wrong...it's the
    only authority I could find. A common C definition of main is:

    main()
    {
    return 0;
    }

    I was trying to find something that speaks to the unspecified nature of
    the accepted parameters in that declaration but couldn't coax google
    into giving me any. The FAQ would lead one to believe it is ok.
    Noah Roberts, Aug 4, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TaiwanNoWhere

    cin and cin.clear() problem

    TaiwanNoWhere, Oct 13, 2003, in forum: C++
    Replies:
    8
    Views:
    4,087
    P.J. Plauger
    Oct 17, 2003
  2. Chris Mantoulidis

    std::cin.ignore() and std::cin.clear()

    Chris Mantoulidis, Jan 6, 2004, in forum: C++
    Replies:
    5
    Views:
    17,132
    Kevin Saff
    Jan 6, 2004
  3. gouki
    Replies:
    3
    Views:
    438
    David Harmon
    Aug 6, 2004
  4. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,657
    Alex Vinokur
    Mar 6, 2005
  5. Fernando
    Replies:
    4
    Views:
    1,641
    Fernando
    Nov 16, 2011
Loading...

Share This Page