getting current timestamp - Unix

This is a discussion on getting current timestamp - Unix ; Hi, I want to find the current time stamp in micro sec precision. The requirement is, my function is executed in an infinite loop. I measure a value and check if the value computed is what i expected 9forget all ...

+ Reply to Thread
Results 1 to 8 of 8

Thread: getting current timestamp

  1. getting current timestamp

    Hi,
    I want to find the current time stamp in micro sec precision.
    The requirement is,
    my function is executed in an infinite loop.
    I measure a value and check if the value computed is what i expected
    9forget all this stuff).
    I want to print the time stamp every time that event occurs.
    So my requirement is printing the current timestamps.
    so I can calculate the average time (in micro secs) after which such
    an event occur...
    I hope I made my requirement clear.
    Can anybody pls help me with this problem.
    I searched many discussion but couldn't find satisfactory answer.
    thanks in advance

  2. Re: getting current timestamp

    I tried following code (for the meanwhile 'm tring to get time in
    mili
    secs)
    long int secs;
    secs=time(NULL);
    this gives me ab error in the line SECS=TIME (NULL)
    error: expected constructor, destructor, or type conversion before
    '=' token
    but if I directly print the value time(null) it works fine.
    so there's some prob with type casting, I guess.
    what can be the solution for this?
    Thanks in advance


  3. Re: getting current timestamp

    A**** Vora wrote:
    > I tried following code (for the meanwhile 'm tring to get time in
    > mili
    > secs)
    > long int secs;
    > secs=time(NULL);


    So why call a function that returns the time in seconds?

    > this gives me ab error in the line SECS=TIME (NULL)
    > error: expected constructor, destructor, or type conversion before
    > '=' token
    > but if I directly print the value time(null) it works fine.
    > so there's some prob with type casting, I guess.


    Total nonsense.

    You obviously didn't compile what you posted.

    Post what you compiled.

    --
    Ian Collins.

  4. Re: getting current timestamp

    On Thu, 18 Sep 2008 01:09:33 -0700, A**** Vora wrote:

    > long int secs;
    > secs=time(NULL);


    > this gives me ab error in the line SECS=TIME (NULL)


    That line isn't in the code above.

    secs=time(NULL);

    is not the same as

    SECS=TIME (NULL)

  5. Re: getting current timestamp

    In <07fd2151-588c-4290-8de6-c0af8a16590d@p31g2000prf.googlegroups.com> A**** Vora writes:

    > I tried following code (for the meanwhile 'm tring to get time in
    > mili
    > secs)
    > long int secs;
    > secs=time(NULL);
    > this gives me ab error in the line SECS=TIME (NULL)
    > error: expected constructor, destructor, or type conversion before
    > '=' token


    Did you include the time.h header file?

    --
    John Gordon A is for Amy, who fell down the stairs
    gordon@panix.com B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"


  6. Re: getting current timestamp

    A**** Vora writes:

    > I want to find the current time stamp in micro sec precision.


    The most portable solution is probably the POSIX function

    clock_getres
    clock_gettime

    The first will tell you resolution and the second one the time. You
    get (on most systems) a number of slightly different clocks like
    CLOCK_REALTIME, CLOCK_MONOTONIC and possibly CLOCK_PROCESS_CPUTIME_ID.

    See "man clock_gettime" to see of you have these available on your
    system.

    --
    Ben.

  7. Re: getting current timestamp

    In article
    <07fd2151-588c-4290-8de6-c0af8a16590d@p31g2000prf.googlegroups.com>,
    A**** Vora wrote:

    > I tried following code (for the meanwhile 'm tring to get time in
    > mili
    > secs)
    > long int secs;
    > secs=time(NULL);
    > this gives me ab error in the line SECS=TIME (NULL)


    No it didn't, because that line doesn't exist in the code you listed.
    C/C++ is case-sensitive. SECS is not the same as secs and TIME is not
    the same as time.


    > error: expected constructor, destructor, or type conversion before
    > '=' token
    > but if I directly print the value time(null) it works fine.
    > so there's some prob with type casting, I guess.
    > what can be the solution for this?


    secs is an int. time does not return an int. Read the man page.

    But you said you wanted millisecond or microsecond accuracy, so why are
    you using a function that will only give you seconds? Use gettimeofday
    or clock_gettime.

  8. Re: getting current timestamp

    On Thu, 18 Sep 2008 01:08:44 -0700 (PDT), A**** Vora wrote:
    > Hi,
    > I want to find the current time stamp in micro sec precision.


    You can use clock_gettime() and clock_getres() for this part.

    > The requirement is, my function is executed in an infinite loop. I
    > measure a value and check if the value computed is what i expected
    > (forget all this stuff). I want to print the time stamp every time
    > that event occurs.


    Don't print the timestamps, but aggregate them while the program runs.
    Even if each function call takes a very small amount of time to
    complete, printing is very likely to:

    * Take much longer to reach stdout or stderr.
    * Be buffered.
    * Take an unpredictabe amount of time.

    > So my requirement is printing the current timestamps.


    This sounds like what you _think_ the requirement is.

    The real requirement seems to be ``to profile a specific function''.
    There are excellent profiling tools out there. Maybe one of them is
    better than printing timestamps?


+ Reply to Thread