Thread: Finding the difference in two times

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

    Finding the difference in two times

    Hello,

    I want to find the difference between two times in C. I am using difftime to calculate the difference but it appears that the results are being returned in seconds. I need to get the exact difference between the two times (nano, micro, milli, etc). Can anyone provide some suggestions on how this can be done?

    Thanks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You're asking for too much precision. There's no function in the C library that does that.
    And I'm not sure there's such a function that gives you all that precision either. There may be, but again, it may not. It depends on the platform and the hardware, most likely.
    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
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The time you get from time() only has second resolution in the first place. There's no way difftime() could hope to get anything better.

    You need platform-specific high-resolution timers. In Windows, you get that from QueryPerformanceCounter(). Under POSIX, you need gettimeofday().
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    17
    Hello, thanks for all your suggestions. I am using Linux. I will think about using POSIX.

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Precise, short-term timing is not possible with a user-mode program on a PC because of the multitasking operating system... While your program is trying to time something, the operating system can interrupt your program to run another process/thread. This can happen even when you are only running one application because the O.S. is always running little background operations.

    With Windows, I think you typically get errors in the ballpark of 30ms, assuming that another program (or driver) isn't hogging the CPU.

    In order to take control away from the operating system, you need to write a kernel mode driver, or you can use a real time operating system (RTOS). There are RTOS versions of Linux, but AFAIK they only run on embedded systems, not on a PC.

    Usually, things that require precise timing (like USB or serial ports, soundcards, video cards, etc.) use a combination of a driver, a separate hardware-clock, and a data-buffer.
    Last edited by DougDbug; 01-24-2008 at 06:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Times function for rational numbers
    By CaliJoe in forum C++ Programming
    Replies: 1
    Last Post: 05-01-2009, 04:09 PM
  2. Replies: 10
    Last Post: 04-26-2009, 02:46 PM
  3. Replies: 6
    Last Post: 08-26-2006, 11:09 PM
  4. Fastest STL container?
    By Shakti in forum C++ Programming
    Replies: 18
    Last Post: 02-17-2006, 02:07 AM
  5. Replies: 10
    Last Post: 11-06-2005, 09:29 PM