hi how do you make it so it does like here it starts the stop watch and then runs whatever code then you stop the timer. I know how to find it out if you have a window open but how do you do it with the main thing.
thanks in advanced,
h3ckf1r3
hi how do you make it so it does like here it starts the stop watch and then runs whatever code then you stop the timer. I know how to find it out if you have a window open but how do you do it with the main thing.
thanks in advanced,
h3ckf1r3
Is this like what you want?
Code:#include <iostream> #include <ctime> int main() { unsigned int start = clock(); std::cout << "waiting for keyhit"; std::cin.ignore(); std::cout << "Time taken in millisecs: " << clock()-start; std::cin.ignore(); return 0; }
use gettimeofday() (portable, recommended) or some Win32 API function to get higher precision.
thanks this is really helpful and cyberfish I am sure the commands you are talking about you would use the same way as mike_g's idea (wich by the way is really goodand I am thankful for it) you would make a variable the current time and then later on print the the new current time - the variable with the old time. I am yet to try it with cyberfish's method and I will try it in a moment.
thanks to all,
h3ckf1r3
note that the clock() function mike_g suggested is implementation specific. It can either measure real time (wall time) or CPU time. If I remember correctly, on Windows it measures real time, while on Linux it measures CPU time (don't know what happens on the Mac, but I would guess it's going to be the same as Linux since they are both POSIX and both use gcc). Therefore, it is ambiguous and I recommend against it.
See
http://www.thescripts.com/forum/thread517364.html
as for the timing method, yes I would do it the way mike_g suggested.
Last edited by cyberfish; 01-05-2008 at 03:10 PM.
well I tried to use yours and just used mik_g's program exept I just changed the clock() function for the timeofday() function and it didnt work. Do you have to use it differently or something?
thanks for all help,
h3ckf1r3
yes you have to use it differently. Google is your friend.
http://www.google.ca/search?hl=en&q=...=Google+Search
here is my "adapter function" that makes it behave like clock() or time()
make sure you include appropriate headers. #include <sys/time.h> IIRC.Code:double current_time () { //returns current time in seconds timeval tv; gettimeofday(&tv, NULL); double rtn_value = (double) tv.tv_usec; rtn_value /= 1e6; rtn_value += (double) tv.tv_sec; return rtn_value; }
note that it returns a double.
not rocket science, probably not even terribly optimized code, but works well enough for me.
I hav a longer but neater form.Sorry i'm an elementary programer
Code:#include<iostream.h> #include<conio.h> #include<dos.h> #include<process.h> #include<iomanip.h> void main() { clrscr(); int l; cout<<"Enter limiting time(in seconds) ";//Time to which the stopwatch works till it automatically shuts off cin>>l; cout<<"Press any key to start. Press pause-break to stop "<<endl; getch(); int m=0,s; a:s=1; b: delay(10); if(s<10) cout<<"\b"<<s; else if(s<100) cout<<"\b\b"<<s; else cout<<"\b\b\b"<<s; s++; if(s%100==0) { m++; cout<<"\b\b\n"<<m<<" "; goto a; } else if(m<l) goto b; else { getch(); exit(0); } getch(); }
That code is useless in its current form. A stopwatch class would be much more useful since you could plug it in at any time you want and gain stopwatch functionality.
2 years bump.
And the code has quite a few problems. Both stylistically and technically. You can start a new thread if you want to learn about them.
Wow didn't see the bump cyberfish. Sorry about that. Closed.