I always get 0 as the result. Thanks.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.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; }
Your interval is too short, try 10000000.
another way is to slow the loop down with some output statements... consider a spinner:note: make sure not to flush the stream on every iteration of the loop (via endl or flush) - that may throw the timing off.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; }
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
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)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; }
> 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.
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