returning milliseconds?

This is a discussion on returning milliseconds? within the C++ Programming forums, part of the General Programming Boards category; what function/header files do you use to return the current time in milliseconds? i thought there was something like java's ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    221

    returning milliseconds?

    what function/header files do you use to return the current time in milliseconds?
    i thought there was something like java's currentTimeMillis() function, but i can no longer remember

    please help.. thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    Depends which OS you're on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    Are you trying to benchmark something?

    If so, then use the ASM 586 instruction RDTSC to get the current number of clock ticks.

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    no im doing an open gl program. trying to do the spinning triangle program. but the problem with that is, on faster systems, it spins really fast. i need a portable way to see how many milliseconds have passed so that it can spin a certain distance. but the problem is, the only standard is with seconds, which does me no good. is there any other solution to this?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    Nice, but you still didn't mention the OS/Compiler.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    I have written a function to use on my Linux systems... Not sure if it would help

    Code:
    double Clock()
    {
        
        struct timeb t; 
        ftime(&t);
        static unsigned int num=0;
        static int flag=0;
        if(flag==0){
        num=t.time;
        flag=1;
        }    
        
        double sec=t.time-num+(t.millitm/1000.0);
        return sec;
    }
    this will return time in terms of seconds.. but it also gives out fractions like 1.23 seconds etc... when first called it starts off from 0.... You might need to include appropriate header files..

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    what function/header files...
    #include <ctime>

    clock() will return the number of "counts" (since your program started).
    CLOCKS_PER_SEC is a system-specific constant for the number of "counts" per second. CLOCKS_PER_SEC is often equal to 1000, which means that clock() is already in milliseconds! But, don't count on that... write good (portable) code!

    There is some information in the Programming FAQ.

    Be aware of a "gotcha" with Windows: clock() will count in milliseconds, but the time_t structure is NOT updated every-single-millisecond by the OS. It jumps in...oh, I forget... something like 30 ms steps. There are other functions (in <windows>) that are more accurate... I think... GetClockTick() ...maybe GetClockCount() ???

  8. #8
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    microsoft compiler/gcc compiler in windows xp
    but this program will be compiled with gcc on windows xp, osx and linux

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>maybe GetClockCount() ???
    GetTickCount()
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    Quote Originally Posted by Hunter2
    >>maybe GetClockCount() ???
    GetTickCount()

    thats defined in windows.h
    which i cant use

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Well, can't think of any standard way of doing it assuming dougdbug is correct about 30ms jumps. I would have suggested QueryPerformanceCounter(), but that's Windows specific too. But heck, if you're doing OpenGL on Windows, why can't you use windows.h?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Here it goes...

    CLOCK
    Syntax

    #include <time.h>

    clock_t clock(void);
    Description

    This function returns the number of clock ticks since an arbitrary time, actually, since the first call to clock, which itself returns zero. The number of tics per second is CLOCKS_PER_SEC.
    Return Value

    The number of tics.
    Portability

    ANSI, POSIX
    Example

    printf("%d seconds have elapsed\n", clock()/CLOCKS_PER_SEC);

    SLEEP
    Syntax

    #include <unistd.h>

    unsigned sleep(unsigned seconds);
    Description

    This function causes the program to pause for seconds seconds.
    Return Value

    The number of seconds that haven't passed (i.e. always zero)
    Portability

    not ANSI, POSIX
    Example

    sleep(5);

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    > but this program will be compiled with gcc on windows xp, osx and linux
    Tough on you - there is no standard function which returns milliseconds on all these platforms.

    So somewhere you're going to have to do something like
    Code:
    #if defined(WINDOWS)
    #include <windows.h>
    int myGetMilliseconds ( void ) {
      return GetTickCount();
    }
    #elif defined(LINUX)
    int myGetMilliseconds ( void ) {
      // find some suitable function
    }
    #else
    #error specify an OS with -DNAME_OF_OS
    #endif
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    Quote Originally Posted by Salem
    > but this program will be compiled with gcc on windows xp, osx and linux
    Tough on you - there is no standard function which returns milliseconds on all these platforms.

    So somewhere you're going to have to do something like
    Code:
    #if defined(WINDOWS)
    #include <windows.h>
    int myGetMilliseconds ( void ) {
      return GetTickCount();
    }
    #elif defined(LINUX)
    int myGetMilliseconds ( void ) {
      // find some suitable function
    }
    #else
    #error specify an OS with -DNAME_OF_OS
    #endif
    thanks, but the clock function will more than due.
    i just needed a value returned that was small enough so that it would be about the same on any system to control motion in opengl

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. importance of returning reference in operator overloading.
    By vaibhavs17 in forum C++ Programming
    Replies: 20
    Last Post: 05-13-2009, 12:28 PM
  2. Time in milliseconds
    By coder_009 in forum C Programming
    Replies: 4
    Last Post: 05-09-2008, 03:36 AM
  3. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 05:38 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Milliseconds
    By Magos in forum C++ Programming
    Replies: 8
    Last Post: 11-01-2001, 02:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21