⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stopwatch.cpp

📁 2002年
💻 CPP
字号:
#include "stopwatch.h"
/*	-*	Stop Watch	*-
* This is a time tool under Windows
* It provides a convenient platform for multi channels and events time mechanism.
*
* The channel mode is cycle-based, which means that the time can be set 
* and retrieved by both channel and cycle provided.
*
* The event mode don't have such time buffer, which can be accessed only by event no.
*/

/*******Stopwatch*****************/
#include "serverparam.h"

Stopwatch World::stopwatch;

Stopwatch::Stopwatch(){
#ifdef WIN32
	TIME_T frq;
	QueryPerformanceFrequency(&frq);
	frequency = frq.QuadPart;
	QueryPerformanceCounter(&inittime);
#else
	gettimeofday(&inittime,NULL);
#endif
	current_cycle = 0;
	correction_factor = 1;
}

void Stopwatch::TimeNow(int no, int cycle){
	TIME_T tmptime;
#ifdef WIN32
	QueryPerformanceCounter(&tmptime);
#else // _LINUX
	gettimeofday(&tmptime,NULL);
#endif
	Channels[no].Setdata(tmptime, cycle);
}

void Stopwatch::TimeEvent(int event){
#ifdef WIN32
	QueryPerformanceCounter(&(Events[event]));
#else // _LINUX
	gettimeofday(&(Events[event]), NULL);
#endif
}

float Stopwatch::MsCheckBetween(int channel1, int channel2, int cycle, float InvalidValue){
	if(!Channels[channel2].IsDataKnown(cycle) || !Channels[channel1].IsDataKnown(cycle))
		return InvalidValue;
	else
		return MsBetween(channel1, cycle, channel2, cycle);
}

float Stopwatch::TimeElapsed(int channel, int cycle){
	TIME_T tmp;
#ifdef WIN32
	QueryPerformanceCounter(&tmp);
#else // _LINUX
	gettimeofday(&tmp, NULL);
#endif
	return MsBetween(Channels[channel].Data(cycle), tmp);
}

float Stopwatch::TimeElapsed_E(int event){
	TIME_T tmp;
#ifdef WIN32
	QueryPerformanceCounter(&tmp);
#else // _LINUX
	gettimeofday(&tmp, NULL);
#endif
	return MsBetween(Events[event], tmp);
}

TIME_T Stopwatch::GetRawData_Event(int event){
	return Events[event];
}

UniformTime Stopwatch::FormatTime(TIME_T& t){
	UniformTime ut;
#ifdef WIN32
	ut.tv.tv_sec = (long)(t.QuadPart / frequency);
	ut.tv.tv_usec = (long)((unsigned long double)(t.QuadPart - ut.tv.tv_sec * frequency) / frequency * 1000000.0f);
#else
	ut.tv = t;
#endif
	return ut;
}

TIME_T Stopwatch::DeFormatTime(UniformTime& t){
	TIME_T tt;
#ifdef WIN32
	tt.QuadPart = (INTEGER64)(t.tv.tv_sec * frequency + t.tv.tv_usec * frequency / 1000000);
#else
	tt = t.tv ;
#endif
	return tt;
}

void Stopwatch::Estimate_correction_factor(){
  float factor = correction_factor * MsSpanCycle(SW_CycleStart, current_cycle) / ServerParam::simulator_step;
  float t = 0.9f;
  // check if we get something meaningful
  if (factor > 0.5 && factor < 1.5){
    /* 
     * There could be other possible weighting. But I think any weighting
     * will be good because there shouldn't be too much ocsilation
     */
    correction_factor = t * correction_factor + (1.0f - t) * factor;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -