Why isn't this working?

This is a discussion on Why isn't this working? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <windows.h> int main(void) { DWORD start = GetTickCount(); for (int i = 0; i < 10000; ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    Why isn't this working?

    Code:
    #include <iostream>
    #include <windows.h>
    
    int main(void)
    {
    	DWORD start = GetTickCount();
    
    	for (int i = 0; i < 10000; i++)
    		;
    
    	std::cout << GetTickCount() - start << std::endl;
    
    	return 0;
    }
    I always get 0 as the result. Thanks.

  2. #2
    Registered User
    Join Date
    Jul 2005
    Posts
    22
    Your interval is too short, try 10000000.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    another way is to slow the loop down with some output statements... consider a spinner:
    Code:
    #include<iostream>
    #include<iomanip>
    #include<ctime>
    
    int main()
    {
            char*spinner="|/-\\";
            time_t start=clock();
    
            std::cout<<spinner[0];  //show the first stage
            for(short int i=0,s=0;i<32000;i++,s++)
            {
                    s=(s>3?0:s);    //if s goes over the bounds of the array, reset it
                    std::cout<<'\b'<<spinner[s];    //delete the char and output a new one
            }
    
            float seconds=(clock()-start)/static_cast<float>(CLOCKS_PER_SEC);       //find the seconds
            std::cout<<"\nTime: "<<std::setprecision(3)<<seconds<<" seconds"<<std::endl;    //ouput the time the program has been running
            return 0;
    }
    note: make sure not to flush the stream on every iteration of the loop (via endl or flush) - that may throw the timing off.
    edit/note2: I used a different method because I'm on linux... the method you used is a non-standard one.
    Last edited by major_small; 07-16-2005 at 11:53 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    Timers

    Code:
    #include<iostream>
    #include <windows.h>
    ////////////
    // Uses QueryPerformanceCounter to
    // demonstrate high accuracy "timers"
    ////////////
    int main()
    {
            
    	
    	LARGE_INTEGER start, finish;
    	QueryPerformanceCounter(&start);
    
    
    	std::cout << start.LowPart << std::endl;
    
    
    	QueryPerformanceCounter(&finish);
    	std::cout << finish.LowPart;
    	return 0;
    }
    That's for very high accuracy timed events, the cout is the most time consuming event there, I wouldn't even know how to practically apply something like this. As to the original, yeah you just need to make your loop bigger, the computer is lightning fast and tickcount will only return something like, what, milliseconds? Whatever it is, you can multiply the (GetTickCount() - StatTickCount) * .001 and you get a nice found answer in seconds. GetTickCount and Win32 Timers should be all you need for general purposes on windows (or just the time.h things you know)

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > for (int i = 0; i < 10000; i++)
    The compiler is likely to get rid of this loop, since it does nothing apart from incrementing i, and that immediately goes out of scope when the loop ends.
    So no matter how big a number you choose, the "loop" might still take a constant (ie 0) time to execute.
    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 major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    arg... foiled by 'optimization' again
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 07:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 07:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM

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