Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411430 Posts in 69363 Topics- by 58416 Members - Latest Member: JamesAGreen

April 19, 2024, 10:48:55 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Linux High-Resolution Timer
Pages: [1]
Print
Author Topic: Linux High-Resolution Timer  (Read 11790 times)
AaronAardvark
Level 1
*


Formerly RazputinOleander


View Profile WWW
« on: May 13, 2009, 10:11:58 PM »

I'm trying ctime or SDL_GetTicks() or anything I can find any info on, but I can't get a high-resolution millisecond count via C++.

Has anyone experienced this?  Anyone have any suggestions?

I'm using Ubuntu and have installed linux-rt and the linux-rt headers, but no dice.

The precision of the time looks fine before calling SDL_Init, but afterwards the value is very small and it just updates about once every second.  Ie., if I have a loop where I continually query the tick count, the output looks like this:

Quote
curr time: 1311, last time: 1307
curr time: 1311, last time: 1307
curr time: 1311, last time: 1307
curr time: 1311, last time: 1307
curr time: 1311, last time: 1307
curr time: 1311, last time: 1307
curr time: 1311, last time: 1307
curr time: 1311, last time: 1307
curr time: 1311, last time: 1307
curr time: 1311, last time: 1307
curr time: 1311, last time: 1307
curr time: 1311, last time: 1307
curr time: 1311, last time: 1307
curr time: 1311, last time: 1307
curr time: 1311, last time: 1307
curr time: 1340, last time: 1307

I'm doing SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); before calling SDL_GetTicks();

I can post my program, but it's hella messy right now.
Logged
Average Software
Level 10
*****

Fleeing all W'rkncacnter


View Profile WWW
« Reply #1 on: May 14, 2009, 04:49:08 AM »

By incredible coincidence, I just had to solve this problem for a job I applied for.

This function basically recreates the win32 QueryPerformanceCounter() on Linux, play with this code:

Code:
#include <sys/time.h>
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <assert.h>

/* Helpful conversion constants. */
static const unsigned usec_per_sec = 1000000;
static const unsigned usec_per_msec = 1000;

/* These functions are written to match the win32
   signatures and behavior as closely as possible.
*/
bool QueryPerformanceFrequency(int64_t *frequency)
{
    /* Sanity check. */
    assert(frequency != NULL);

    /* gettimeofday reports to microsecond accuracy. */
    *frequency = usec_per_sec;

    return true;
}

bool QueryPerformanceCounter(int64_t *performance_count)
{
    struct timeval time;

    /* Sanity check. */
    assert(performance_count != NULL);

    /* Grab the current time. */
    gettimeofday(&time, NULL);
    *performance_count = time.tv_usec + /* Microseconds. */
                         time.tv_sec * usec_per_sec; /* Seconds. */

    return true;
}

It's in C99, but you should be able to convert it to C++ fairly easily.
Logged



What would John Carmack do?
Business Bear
Level 0
***



View Profile
« Reply #2 on: May 14, 2009, 06:11:44 AM »

Let me add to this crazy coincidence.  I did something like this last night for a Linux runtime of my engine.  I was basically trying to create an event loop for X that had a game update timer.  This forum thread helped me out immensely (he gives the code at the end):

www.linuxquestions.org/questions/programming-9/xnextevent-select-409355/
Logged
AaronAardvark
Level 1
*


Formerly RazputinOleander


View Profile WWW
« Reply #3 on: May 14, 2009, 06:47:47 AM »

Awesome!
Thanks guys!
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic