unbelievable!!!

This is a discussion on unbelievable!!! within the C++ Programming forums, part of the General Programming Boards category; Code: . . time (&start); for(i= 0; i < 110; i++) { for(j= 0; j< 110; j++) { cout << ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    50

    Thumbs up unbelievable!!!

    Code:
       .
       .
    time (&start);
    
    for(i= 0; i < 110; i++)
    {
       for(j= 0; j< 110; j++)
        {
            cout << i ;
            cout << j;
            .
            .
            .
         }
    }
     
    time (&end);
    dif = difftime (end,start);
       .
       .
    Hi,
    I wanted to measure time for my double loop to run its course . What shocked me is the fact that diff is longer (greater) when I comment out these two "cout lines" all other parameters being equal. How is it possible?
    Thank you

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    That really doesn't make any sense. You sure about it?

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, time() is a very imprecise way to measure time... clock() gives a much better resolution. How many times did you test it?

    If the difference is one second (which is the resolution of time), and the additional couting by itself takes less than a second, it is entirely possible that in one case you started measuring as the second was just about to end and in the second case when the second had just begun. Or some other process may have kicked in and your program had to share time with it.
    Last edited by anon; 04-25-2007 at 04:48 AM.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    50
    Yes I am sure I did not even touch my mouse during the running. I tested it more than 5 times.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    1. Post an actual program which shows this effect, not something full of mystery . . .
    2. State your OS and compiler (and versions)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    In my experience std::cout makes programs really really slow. I often use std::cout for debugging purposes (not sure how to use the debugger). In the program I am currently working on it increased the running time from a few seconds to about 5 min.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,954
    If you tried to display your text over a network, chances are it would be even slower. I/O tends to be slow relative to say, arithmetic calculations, but if you need to do it, you need to do it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Well 110*110 = 12100 basic operations (I'm assuming the inner loop doesn't do much though), which is hardly much for a modern computer. Try doing at least 5 million basic operations and the std::cout version would most likely be slower. Also std::cout is may be slow because by default it has synchronization with stdio functions (like printf()), you can disable this with a ios_base::sync_with_stdio(false) which should speed things up a notch.
    The cost of software maintenance increases with the square of the programmer's creativity.

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    50
    I am not trying to conceal anything, but simply posting the content of the loop would just distract the attention from the main problem which is the shorter time when I use "cout lines" comparing to that when I comment the line out.
    OS:Windows XP, Compiler Visual Studio 2005.

  10. #10
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    change it from 110 to 10000 and test again, you'll notice the difference.

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I am not trying to conceal anything, but simply posting the content of the loop would just distract the attention from the main problem which is the shorter time when I use "cout lines" comparing to that when I comment the line out.
    ... in which case no-one else can reproduce the behaviour. It's kind of hard to explain something you have never seen.

    In any case, don't use time() to measure program performance. It is meant for getting the day of the year, and the time of the day, but not for measuring the super-fast processes. It's like timing a 100 m race by looking at the second hand of your wristwatch.

  12. #12
    Registered User
    Join Date
    Dec 2004
    Posts
    50
    Then, What function should I use to test this loop?
    Or in general What functions should I use to measure performance of a program?
    Last edited by strickey; 04-26-2007 at 04:40 AM. Reason: Adding a line

  13. #13
    Registered User
    Join Date
    Dec 2004
    Posts
    50
    Quote Originally Posted by anon View Post
    ... in which case no-one else can reproduce the behaviour. It's kind of hard to explain something you have never seen.
    It would be very tedious to reproduce the behaviour because inside the loop I use some methods from open source and that I developed myself so lot of time woudl be gone just to collect all necesaary libraries. Again, whatever I use in the loop does not influence output stream. I am just perplexed with this outcome.

  14. #14
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Use clock to measure performance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i'm fed up with the #$&#$% open dialog
    By Yarin in forum Windows Programming
    Replies: 36
    Last Post: 02-17-2008, 07:10 AM
  2. Unbelievable wind speeds
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 09-09-2004, 03:42 PM
  3. Analysis of Top Programming Languages Companies Need :: Inbelievable!
    By kuphryn in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 01-06-2003, 03:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21