Time in milliseconds

This is a discussion on Time in milliseconds within the C Programming forums, part of the General Programming Boards category; Hi, I've searched the topic on google and this website, but i could not find the exact answer what i ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    8

    Angry Time in milliseconds

    Hi,

    I've searched the topic on google and this website, but i could not find the exact answer what i was looking for.

    For a certain scenario in my application (timestamps related) i need time in milliseconds. What i am currently doing is to get time in seconds using time() function and convert it into milliseconds by multiplying it with 1000.

    But the problem is that time_t is signed long (32 bit). When i multiply time_in_seconds with 1000, it overflows 32 bits and the resulting time in milliseconds becomes -ve.

    Please tell me how can i get time in milliseconds ? Also, i don't want processor time taken, i want wallclock time.

    OS: Unix


    Thanx

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    If you only need time(), then cast it to unsigned long long, then multiply it by 1000. That way it won't overflow.

    But... it seems you might need more precision if you're targeting milliseconds...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps you can use long-long. In at least Linux, you have gettimeofday - it may exist in other Unix variants too. It gives you time in microseconds (although it may jump in steps of several microseconds at a time).

    --
    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.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    8
    Isn't there any function on Unix or Linux that gives time from start of the process rather than the epoch? Such a long time period from 1970 to now makes it so big in magnitude.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by coder_009 View Post
    Isn't there any function on Unix or Linux that gives time from start of the process rather than the epoch? Such a long time period from 1970 to now makes it so big in magnitude.
    Just take the time at the start of main, and then show the difference between that and "current time" when you want to show how long you've been running [or some 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execution Time - Rijandael encryption
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2008, 09:25 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. Journey time prog 1 minute wrong
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 10-12-2006, 03:41 AM
  4. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:12 PM

Tags for this Thread


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