realtime HighPerformanceCounter information

Chad J gamerChad at _spamIsBad_gmail.com
Tue Nov 21 17:47:40 PST 2006


nobody_ wrote:
>>>Is it possible to get timing information before stopping a timer?
>>>
>>>Something like this:
>>>
>>>HighPerformanceCounter c = new HighPerformanceCounter();
>>>c.start();
>>>while(c.microseconds()<1000){
>>>pauze();
>>>}
>>>c.stop();
>>>
>>>When I do this now, c.microseconds() returns some 'random number :(
>>
>>I'm curious, why do you need to keep the timer running?
> 
> 
> I hoped to use it for timing my main loop :) 
> 
> 

Hmmm, well unless there are bizarre realtime low-level type conditions 
that must be met, then I'd suggest doing what Lutger just mentioned - 
wrap D's timer into your own that allows you to do what you want.
Perhaps something like this:

class MyTimer
{
	private HighPerformanceCounter hpc;
	private ulong runningTime = 0;
	
	public ulong microseconds()
	{
		hpc.stop();
		runningTime += hpc.microseconds();
		hpc.start();
		return runningTime;
	}
	
	public void reset()
	{
		hpc.stop();
		hpc.start();
		runningTime = 0;
	}
	
	this()
	{
		hpc = new HighPerformanceCounter();
		hpc.start();
	}
}

Now your code becomes something like this:

MyTimer timer = new MyTimer();
while( timer.microseconds < 1000 )
{
	// ... do whatever ...
}

IMO Phobo's current timer setup is a bit too low level and barely 
documented for normal use.  std.perf is also a poor name for a timing 
module.  It'd be nice to see a timer class/struct similar to the one I 
just wrote (but much much more feature rich of course) implemented in 
phobos, perhaps in a seperate std.timer module (which means std.perf can 
stay as it is, and it probably won't bother anyone).



More information about the Digitalmars-d-learn mailing list