New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Using gettimeofday to compute reaction times


Post Reply New Thread Subscribe

 
Thread Tools
MichiganDavid

 
MichiganDavid's Avatar
 
Member Since: Jan 21, 2009
Location: Midland, Michigan
Posts: 9
MichiganDavid is on a distinguished road
Mac Specs: MB Pro, 17", 2.5 GHz Intel

MichiganDavid is offline
[reference to http://www.mac-forums.com/forums/os-...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.
QUOTE Thanks
deer dance

 
deer dance's Avatar
 
Member Since: Jan 23, 2009
Location: Indiana
Posts: 162
deer dance has a little shameless behaviour in the past
Mac Specs: Soon to own

deer dance is offline
So, you're trying to get us to do your son's homework for him?


Proud Python Programmer/HTML Expert
Believer in Mac OS 7
QUOTE Thanks
MichiganDavid

 
MichiganDavid's Avatar
 
Member Since: Jan 21, 2009
Location: Midland, Michigan
Posts: 9
MichiganDavid is on a distinguished road
Mac Specs: MB Pro, 17", 2.5 GHz Intel

MichiganDavid is offline
Quote:
Originally Posted by deer dance View Post
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.
QUOTE Thanks
deer dance

 
deer dance's Avatar
 
Member Since: Jan 23, 2009
Location: Indiana
Posts: 162
deer dance has a little shameless behaviour in the past
Mac Specs: Soon to own

deer dance is offline
Does't that seemlike a logical idea?


Proud Python Programmer/HTML Expert
Believer in Mac OS 7
QUOTE Thanks

Post Reply New Thread Subscribe


« Ruby with MacPorts in bash help? | Compiling for Release on PPC Architecture »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Times RSS reader goobimama OS X - Apps and Games 4 05-14-2008 03:47 PM
Video loading and file transfer times etc Jamiert Apple Notebooks 0 09-27-2007 12:51 PM
Editing DVD Chapter times?? rhapsody8 Movies and Video 2 07-26-2007 03:34 PM
How Many times Superide Apple Notebooks 18 06-04-2007 10:21 PM
batt. maintence - keep it on at all times? keep adapter on all time? whats do you do pkny Apple Notebooks 6 03-05-2005 03:23 PM

All times are GMT -4. The time now is 04:43 PM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?