Thread: Time function in C

  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,732
    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,732
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  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, 05: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, 02:46 PM
  4. Replies: 5
    Last Post: 02-08-2003, 07: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