Time function in C

This is a discussion on Time function in C within the C Programming forums, part of the General Programming Boards category; I'm working in C and I need to get current time in miliseconds. Time.h can only return time in seconds. ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    4

    Time function in C

    I'm working in C and I need to get current time in miliseconds. Time.h can only return time in seconds. Does anyone know which function, or library could do that?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What OS?

    time() is a generic function that is portable, but there are several different ways to get time in various systems. Also, if you want to measure "CPU time", rather than wall-clock time, have a look at:
    http://www.hmug.org/man/3/clock.php

    The latter is useful for benchmarking and such.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    4
    i'm working in ubuntu. i need one program(server) to send a message containing its current time in miliseconds to another client, which should then return the message so that the server could measure the latency.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    have a look at this FAQ

    ssharish

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    54
    Quote Originally Posted by ssharish2005 View Post
    have a look at this FAQ

    ssharish
    That faq says nothing about milliseconds.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    The main point of that FAQ is to look into the usage if Clock function. That gives an idea of millionth second.

    ssharish

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    4
    clock won't work. frist value returned is 0, i sent it to the client, made client wait for 1 sec before sending it back. then i used clock function again and the new value returned was 0 again.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,336
    gettimeofday() returns a struct containing seconds and microseconds.
    Though it is down to your implementation to define the resolution at which it updates the microsecond field.
    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.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    4
    it works! Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  2. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Replies: 5
    Last Post: 02-08-2003, 06:42 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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