File: timer.c

package info (click to toggle)
scilab 4.0-12
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 100,640 kB
  • ctags: 57,333
  • sloc: ansic: 377,889; fortran: 242,862; xml: 179,819; tcl: 42,062; sh: 10,593; ml: 9,441; makefile: 4,377; cpp: 1,354; java: 621; csh: 260; yacc: 247; perl: 130; lex: 126; asm: 72; lisp: 30
file content (196 lines) | stat: -rw-r--r-- 5,656 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*-----------------------------------------------------------------------------------*/
/* Copyright INRIA/ENPC */
/*-----------------------------------------------------------------------------------*/
#include <stdio.h>

#if ~defined(THINK_C) && ~defined(__MWERKS__)
	#if !(defined __MSC__) && !(defined __MINGW32__) 
		#include <sys/time.h>
	#else 
		#include <windows.h>
		#include <winbase.h> /* header du compteur haute rsolution */
	#endif 
#endif 

#include <time.h>
#include "../machine.h"

#if defined(THINK_C) || defined(__MWERKS__)
	#include <Threads.h> 
#endif
/*-----------------------------------------------------------------------------------*/
#define DT_TIMER 10000

#ifndef CLOCKS_PER_SEC
	#if defined(sun)
		#define CLOCKS_PER_SEC 1000000
	#endif
#endif
/*-----------------------------------------------------------------------------------*/
#if WIN32
	static __int64 i64UserTick1;
	static LARGE_INTEGER   Tick1;
	typedef enum
  {
    WINNT,	WIN2K_XP, WIN9X, UNKNOWN
  }PLATFORM;
#else
	static clock_t t1;
#endif

static int init_clock = 1;

#if WIN32
	PLATFORM GetPlatform(void);
	static long stimerwin(void);
#endif
/*-----------------------------------------------------------------------------------*/
#if WIN32
PLATFORM GetPlatform()
{
  OSVERSIONINFO osvi;
  osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  if (!GetVersionEx(&osvi)) return UNKNOWN;
  switch (osvi.dwPlatformId)
  {
    case VER_PLATFORM_WIN32_WINDOWS: return WIN9X;
    case VER_PLATFORM_WIN32_NT: 
			if (osvi.dwMajorVersion == 4)
				return WINNT;
      else
				return WIN2K_XP;
  }
  return UNKNOWN;
}
#endif
/*-----------------------------------------------------------------------------------*/
/* Allan CORNET 2004 */
int C2F(timer)(double *etime)
{
#if WIN32 
  if (GetPlatform() == WIN9X)
  {
    /* Timer high Precision */
    /* It is not easy to have CPU Time on Win9x */
    /* timer don't return CPU Time but time between 2 calls of Timer() 2.7 compatibility*/
    LARGE_INTEGER   Tick2;
    LARGE_INTEGER   freq;
      
    QueryPerformanceFrequency(&freq);
    QueryPerformanceCounter(&Tick2);
      
    if (init_clock == 1) {init_clock = 0; Tick1 = Tick2;}
    *etime=(double) ( (double) (Tick2.QuadPart - Tick1.QuadPart) / (double)(freq.QuadPart));
    Tick1 = Tick2;
  }
  else
  if (GetPlatform() == UNKNOWN)
  {
		*etime=(double)-1;
  }
  else
  {
		/* NT 3.5 & > */
		/* Return CPU Time */
		FILETIME  ftCreation, ftExit, ftKernel,  ftUser;
   	__int64 i64UserTick2;
	
		GetProcessTimes(GetCurrentProcess(), &ftCreation, &ftExit, &ftKernel, &ftUser);
		i64UserTick2=*((__int64 *) &ftUser);
		if (init_clock == 1) {init_clock = 0; i64UserTick1 = i64UserTick2;}
		*etime=(double) ((double)(i64UserTick2 - i64UserTick1)/(double)10000000U);
		i64UserTick1 = i64UserTick2;
	}
#else
  clock_t t2;
  t2 = clock();
  if (init_clock == 1) {init_clock = 0; t1 = t2;}
  *etime=(double)((double)(t2 - t1)/(double)CLOCKS_PER_SEC);
  t1 = t2;
#endif
  return(0);
}
/*-----------------------------------------------------------------------------------*/
/* define X_GETTIMEOFDAY macro, a portable gettimeofday() */
#if  defined(VMS)
	#define X_GETTIMEOFDAY(t) gettimeofday(t)
#else
	#if defined(THINK_C) || defined(__MWERKS__)
		#define X_GETTIMEOFDAY(t) 0 
	#else
		#if defined(WIN32)
			#if !(defined __MSC__)
				#ifndef  __MINGW32__
					#define X_GETTIMEOFDAY(t) gettimeofday(t, &tmz )
					static struct timezone tmz;
				#else
					#define X_GETTIMEOFDAY(t) 0
				#endif /** __MINGW32__ **/
			#else
				#define X_GETTIMEOFDAY(t) 0
			#endif /* MSC__ */
		#else
			#define X_GETTIMEOFDAY(t) gettimeofday(t, (struct timezone*)0)
		#endif
	#endif
#endif 
/*-----------------------------------------------------------------------------------*/
/* this function is no more used  */
/*-----------------------------------------------------------------------------------*/
static long int scilab_stimer_deprecated(void)
{
#if defined(THINK_C)||defined(__MWERKS__) 
        YieldToAnyThread();
        return(0);
#else 
#if !(defined __MSC__) && !(defined __MINGW32__)
  struct timeval ctime;
  X_GETTIMEOFDAY(&ctime);
  scilab_timer_check();
  return(ctime.tv_usec);
#else 
  return(stimerwin());
#endif /* !(defined __MSC__) && !(defined __MINGW32__) */ 
#endif /* defined(THINK_C)||defined(__MWERKS__) */
}
/*-----------------------------------------------------------------------------------*/
/* stimer */
/*-----------------------------------------------------------------------------------*/
#if WIN32
static long stimerwin(void)
{
	long int i;
	union {FILETIME ftFileTime;__int64  ftInt64;} ftRealTime; 
	GetSystemTimeAsFileTime(&ftRealTime.ftFileTime); 
	i= (int) (ftRealTime.ftInt64  & ((LONGLONG) 0x0ffffffff));
	return( i/10); /** convert to microseconds **/
}
#endif
/*-----------------------------------------------------------------------------------*/
/* returns 1 if interval from last call is greater than 
 * a time interval of dt microsec (dt=10000)
 */
/*-----------------------------------------------------------------------------------*/
#if WIN32
static long int ctime_old=0;
int scilab_timer_check(void)
{
	int rep;
	long int ctime = stimerwin();
	rep = ( ctime - ctime_old > DT_TIMER ) ? 1 : 0 ;
	ctime_old=ctime;
	return rep;
}
#else 
int scilab_timer_check(void)
{
  int rep;
  static struct timeval ctime_old;
  struct timeval ctime;
  X_GETTIMEOFDAY(&ctime);
  rep = (ctime.tv_sec > ctime_old.tv_sec) ? 1  : ( ctime.tv_usec - ctime_old.tv_usec > DT_TIMER ) ? 1 : 0 ;
  if (rep) ctime_old=ctime;
  return rep;
}
#endif /* WIN32  */
/*-----------------------------------------------------------------------------------*/