Using gettimeofday to compute reaction times

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.
 
Joined
Jan 23, 2009
Messages
162
Reaction score
1
Points
18
Location
Indiana
Your Mac's Specs
Soon to own
So, you're trying to get us to do your son's homework for him?
 
OP
MichiganDavid
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
So, you're trying to get us to do your son's homework for him?

Well, . . . not exactly. This piece makes life easier, but he can always revert to the stopwatch. :Mischievous:
 
Joined
Jan 23, 2009
Messages
162
Reaction score
1
Points
18
Location
Indiana
Your Mac's Specs
Soon to own
Does't that seemlike a logical idea?
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top