Jump to content

High-precision time


Josh
 Share

Recommended Posts

This is what I use for some internal timing stuff:
 

#ifdef _WIN32
	double PCFreq = 0.0;
	__int64 CounterStart = 0;
	bool counterstarted = false;
#endif

	double fMillisecs()
	{
#ifdef _WIN32
		//return GetTickCount64(); // this only updates every 16 milliseconds, don't use it!
		//return timeGetTime();
		if (!counterstarted)
		{
			LARGE_INTEGER li;
			QueryPerformanceFrequency(&li);
			PCFreq = double(li.QuadPart) / 1000.0;
			QueryPerformanceCounter(&li);
			CounterStart = li.QuadPart;
			counterstarted = true;
		}
		LARGE_INTEGER li;
		QueryPerformanceCounter(&li);
		//return (((double(li.QuadPart - CounterStart) / PCFreq) * 1000.0)) / 1000.0;
		return double(li.QuadPart - CounterStart) / PCFreq;
#else
		long t;
		struct timeval tv;
		gettimeofday(&tv, 0);
		long secstomillisecs = 1000;
		t = tv.tv_sec * secstomillisecs;
		t += tv.tv_usec / 1000.0;
		return t;
#endif
	}

 

  • Like 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Thats what Canardia uses, but i was not sure if it's fine to use for not profiling purposes but i see it is now. His code:

// Stopwatch.h
#pragma once
class Stopwatch
{
public:
    LARGE_INTEGER StartTime, Frequency, StopTime, ElapsedTime;
    Stopwatch();
    void Start();
    double Elapsed();
};

// Stopwatch.cpp
#include "UltraEngine.h"
#include "Stopwatch.h"
Stopwatch::Stopwatch()
{
    QueryPerformanceFrequency(&Frequency);
}
void Stopwatch::Start()
{
    QueryPerformanceCounter(&StartTime);
}
double Stopwatch::Elapsed()
{
    QueryPerformanceCounter(&StopTime);
    ElapsedTime.QuadPart = StopTime.QuadPart - StartTime.QuadPart;
    return (double)(ElapsedTime.QuadPart) / (Frequency.QuadPart);
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...