C ++ time, milliseconds from the last whole second

I am working on C ++, an application that needs detailed time information, down to the millisecond level.

We intend to collect time for second accuracy using the standard function time()in <ctime>. We would like to additionally collect the milliseconds that have passed since the last second, given time().

Does anyone know a convenient method to get this information?

+5
source share
10 answers

Boost.DateTime has millisecond and nanosecond IF representations , supporting their underlying platform. Although it uses platform-specific code, it saves this data from your code.

If it matters a lot, they have another way to make a platform-independent subsecond decision. This page in pairs below talks about how to do this.

(From page)

For example, suppose we want to build using a counter that represents tenths of a second. That is, each tick is 0.1 second.

int number_of_tenths = 5;
//create a resolution independent count -- divide by 10 since there are 
//10 tenths in a second.  
int count = number_of_tenths*(time_duration::ticks_per_second()/10);
time_duration td(1,2,3,count); //01:02:03.5 //no matter the resolution settings
+11
source

, ANSI C . Windows, GetTickCount(), timeGetTime() QueryPerformanceCounter()/QueryPerformanceFrequency(). , .

; , .

+8

GetTickCount Windows gettimeofday * nix QueryPerformanceCounter Windows ( GetTickCount )

+3

, Intel

Intel, . , . , , , .

, . linux /proc/cpuinfo , . .

,

11867927879484732
11867927879692217
it took this long to call printf: 207485

Intel, .

#include < stdio.h >  // stackoverflow bug: pre tag eats the filenames,
#include < stdint.h > // so i had to put spaces in the angle brackets

inline uint64_t rdtsc() {
    uint32_t lo, hi;
    __asm__ __volatile__ (
      "xorl %%eax, %%eax\n"
      "cpuid\n"
      "rdtsc\n"
      : "=a" (lo), "=d" (hi)
      :
      : "%ebx", "%ecx");
    return (uint64_t)hi << 32 | lo;
}

main()
{
    unsigned long long x;
    unsigned long long y;
    x = rdtsc();
    printf("%lld\n",x);
    y = rdtsc();
    printf("%lld\n",y);
    printf("it took this long to call printf: %lld\n",y-x);
}
+2

, .

( msv++ linux/g++) , QueryPerformanceCounter Windows gettimeofday Linux. , . , .

#if defined(_MSC_VER)
#  define NOMINMAX // workaround un bug dans windows.h qui define des macros
                   // "min" et "max" qui entrent en conflit avec la STL.
#  include <windows.h>
#else
#  include <sys/time.h>
#endif

namespace Utils
{

   /**
    * Implémente un chronométre qui mesure le temps etre deux appels.
    */
   class CTimer
   {
   private:
#     if defined(_MSC_VER)
         LARGE_INTEGER m_depart;
#     else
         timeval m_depart;
#     endif

   public:
      /**
       * Démarre le timer.
       * 
       * Cette fonction sert à démarrer le timer. Si le timer est  déja
       * démarrer, elle le redémarre simplement.
       */
      inline void start()
      {
#        if defined(_MSC_VER)
            QueryPerformanceCounter(&m_depart);
#        else
            gettimeofday(&m_depart, 0);
#        endif

      };

      /**
       * Retourne le nombre de secondes depuis le départ du timer.
       * 
       * @return Nombre de secondes écoulés depuis le départ du timer
       */
      inline float GetSecondes() const
      {
#        if defined(_MSC_VER)
            LARGE_INTEGER now;
            LARGE_INTEGER freq;

            QueryPerformanceCounter(&now);
            QueryPerformanceFrequency(&freq);

            return (now.QuadPart - m_depart.QuadPart) / static_cast<float>(freq.QuadPart);
#        else
            timeval now;
            gettimeofday(&now, 0);

            return now.tv_sec - m_depart.tv_sec + (now.tv_usec - m_depart.tv_usec) / 1000000.0f;
#        endif
      };
   };
}
+2

Unix, gettimeofday() , .

int gettimeofday(struct timeval *tv, struct timezone *tz);

struct timeval {
    time_t      tv_sec;     /* seconds */
    suseconds_t tv_usec;    /* microseconds */
};
+1
#include <ctime>

clock_t elapsed = static_cast<double>(clock() / CLOCKS_PER_SEC);

.

, , .

+1

Everything that is not in the header (c)time.hrequires methods specific to the OS. I believe that all these methods are the second resolution.

What OS do you work in?

0
source

Take a look at the QueryPerformanceCounter methods if it is for Windows.

-1
source

Intel's Threading Building Blocks library has a function for this, but TBB is currently only available for Intel and clones (that is, it is not available on SPARC, PowerPC, ARM, etc.)

-1
source

All Articles