source: trunk/src/core/time.cc @ 380

Last change on this file since 380 was 380, checked in by epyon, 10 years ago
  • oops, missed src : got rid of to_string and other std::string utilities (except slurp) string no longer in nv namespace
File size: 2.8 KB
Line 
1// Copyright (C) 2011-2014 ChaosForge Ltd
2// This file is part of NV Libraries.
3// For conditions of distribution and use, see copyright notice in nv.hh
4
5#include "nv/core/time.hh"
6
7#include "nv/core/logging.hh"
8
9#include <ctime>
10
11#if NV_COMPILER == NV_MSVC
12#define WIN32_LEAN_AND_MEAN
13#include <windows.h>
14#include <intrin.h>
15#pragma intrinsic(__rdtsc)
16#else
17#if NV_COMPILER == NV_GNUC && NV_PLATFORM == NV_WINDOWS
18// mingw doesn't have usleep nor nanosleep...
19#include <windows.h>
20#endif
21// #include <unistd.h>
22// #include <time.h>
23// #include <sys/time.h>
24// #include <sys/types.h>
25#include <stdio.h>
26#include <time.h>
27#include <sys/time.h>
28#endif
29
30
31
32struct timer_impl
33{
34        timer_impl()
35        {
36                clock_zero = clock();
37#if NV_COMPILER == NV_MSVC
38                QueryPerformanceFrequency(&frequency);
39                QueryPerformanceCounter(&query_zero);
40#else
41                gettimeofday(&timeval_zero, NULL);
42#endif
43        }
44        clock_t clock_zero;
45#if NV_COMPILER == NV_MSVC
46        LARGE_INTEGER query_zero;
47        LARGE_INTEGER frequency;
48#else
49        struct timeval timeval_zero;
50#endif
51};
52
53static timer_impl zero_timer;
54
55nv::uint64 nv::get_ticks()
56{
57#if NV_COMPILER == NV_MSVC
58        return __rdtsc();
59#else
60        register long long ticks asm("eax") = 0;
61        asm volatile (".byte 15, 49" : : : "eax", "edx");
62        return static_cast<nv::uint64>( ticks );
63#endif
64}
65
66void nv::sleep( uint32 ms )
67{
68#if NV_COMPILER == NV_MSVC
69        Sleep( ms );
70#else
71#if NV_COMPILER == NV_GNUC && NV_PLATFORM == NV_WINDOWS
72        Sleep( ms );
73#else
74        struct timespec ts;
75        ts.tv_sec = 0;
76        ts.tv_nsec = (long)ms * 1000000;
77        nanosleep(&ts, NULL);
78//      usleep( ms * 1000 );
79#endif
80#endif
81}
82
83nv::uint32 nv::get_cpu_ms()
84{
85        return (uint32)( (f32)( clock() - zero_timer.clock_zero ) / ( (f32)CLOCKS_PER_SEC / 1000.0 ) ) ;
86}
87
88nv::uint64 nv::get_cpu_us()
89{
90        return (uint64)( (f32)( clock() - zero_timer.clock_zero ) / ( (f32)CLOCKS_PER_SEC / 1000000.0 ) ) ;
91}
92
93nv::uint32 nv::get_system_ms()
94{
95#if NV_COMPILER == NV_MSVC
96        LARGE_INTEGER now;
97        QueryPerformanceCounter(&now);
98        LONGLONG result = now.QuadPart - zero_timer.query_zero.QuadPart;
99        return (uint32) (1000.0 * result / (double) zero_timer.frequency.QuadPart);
100#else
101        struct timeval now;
102        gettimeofday(&now, NULL);
103        return (uint32)( (now.tv_sec - zero_timer.timeval_zero.tv_sec)*1000+(now.tv_usec-zero_timer.timeval_zero.tv_usec)/1000 );
104#endif
105}
106
107nv::uint64 nv::get_system_us()
108{
109#if NV_COMPILER == NV_MSVC
110        LARGE_INTEGER now;
111        QueryPerformanceCounter(&now);
112        LONGLONG result = now.QuadPart - zero_timer.query_zero.QuadPart;
113        return (uint64) (1000000.0 * result / (double) zero_timer.frequency.QuadPart);
114#else
115        struct timeval now;
116        gettimeofday(&now, NULL);
117        return (uint32)( (now.tv_sec - zero_timer.timeval_zero.tv_sec)*1000000+(now.tv_usec - zero_timer.timeval_zero.tv_usec) );
118#endif
119}
Note: See TracBrowser for help on using the repository browser.