- Joined
- Jan 21, 2009
- Messages
- 9
- Reaction score
- 0
- Points
- 1
- Location
- Midland, Michigan
- Your Mac's Specs
- MB Pro, 17", 2.5 GHz Intel
[reference to http://www.mac-forums.com/forums/os-x-darwin-development/38816-issue-using-times-function-intel-mac.html.]
I am (trying to) help my seventh-grade son with a project. He needs to record reaction time to at least millisecond precision. I have discovered that the time() function only reports to integer seconds precision, but it looks like gettimeofday() will do what I want. I am very inexperienced with C++ programming, however, so if it's not too much trouble, could someone post a sample code for me? Here's basically what I'm trying right now:
#include <iostream>
#include <time.h>
#include <sys/time.h>
using namespace std;
/* time example */
double time_inSeconds()
//define a function to return the time in decimal seconds
{
double seconds;
timeval t;
gettimeofday(&t,NULL);
seconds = t.tv_sec+t.tv_usec/1000000;
return seconds;
}
int main ()
{
double startTime, endTime, elapsedTime;
startTime=time_inSeconds();
printf("press return");
//stuff happens
getchar();
endTime=time_inSeconds();
elapsedTime=endTime-startTime;
cout << ("Elapsed time is ", elapsedTime);
return 0;
}
When I run this code, the printed output is an integer, not the decimal value that he needs.
Thanks.
I am (trying to) help my seventh-grade son with a project. He needs to record reaction time to at least millisecond precision. I have discovered that the time() function only reports to integer seconds precision, but it looks like gettimeofday() will do what I want. I am very inexperienced with C++ programming, however, so if it's not too much trouble, could someone post a sample code for me? Here's basically what I'm trying right now:
#include <iostream>
#include <time.h>
#include <sys/time.h>
using namespace std;
/* time example */
double time_inSeconds()
//define a function to return the time in decimal seconds
{
double seconds;
timeval t;
gettimeofday(&t,NULL);
seconds = t.tv_sec+t.tv_usec/1000000;
return seconds;
}
int main ()
{
double startTime, endTime, elapsedTime;
startTime=time_inSeconds();
printf("press return");
//stuff happens
getchar();
endTime=time_inSeconds();
elapsedTime=endTime-startTime;
cout << ("Elapsed time is ", elapsedTime);
return 0;
}
When I run this code, the printed output is an integer, not the decimal value that he needs.
Thanks.