Results 1 to 4 of 4
  1. #1

    MichiganDavid's Avatar
    Member Since
    Jan 21, 2009
    Location
    Midland, Michigan
    Posts
    9
    Specs:
    MB Pro, 17", 2.5 GHz Intel
    Question Using gettimeofday to compute reaction times
    [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.

  2. #2

    deer dance's Avatar
    Member Since
    Jan 23, 2009
    Location
    Indiana
    Posts
    162
    Specs:
    Soon to own
    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

  3. #3

    MichiganDavid's Avatar
    Member Since
    Jan 21, 2009
    Location
    Midland, Michigan
    Posts
    9
    Specs:
    MB Pro, 17", 2.5 GHz Intel
    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.

  4. #4

    deer dance's Avatar
    Member Since
    Jan 23, 2009
    Location
    Indiana
    Posts
    162
    Specs:
    Soon to own
    Does't that seemlike a logical idea?

    Proud Python Programmer/HTML Expert
    Believer in Mac OS 7

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. iPhone street reaction
    By brendan67 in forum Schweb's Lounge
    Replies: 2
    Last Post: 09-13-2012, 05:10 PM
  2. iPhone street reaction
    By brendan67 in forum iPhone Hardware and Accessories
    Replies: 0
    Last Post: 09-13-2012, 04:08 PM
  3. Rebuilding the way we compute.
    By Village Idiot in forum Schweb's Lounge
    Replies: 7
    Last Post: 05-19-2009, 01:11 PM
  4. Market Reaction to new Notebooks
    By fleurya in forum Schweb's Lounge
    Replies: 35
    Last Post: 11-04-2008, 12:53 AM
  5. dead screen area - no reaction to clicks...
    By fsiedlok in forum Apple Notebooks
    Replies: 6
    Last Post: 10-14-2004, 02:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •