# Suggestion : microseconds timer

**URL:** <https://forum.juce.com/t/suggestion-microseconds-timer/4871>\
**Category:** General JUCE discussion\
**Created:** [February 10, 2010, 10:04am UTC](https://forum.juce.com/t/suggestion-microseconds-timer/4871 "2010-02-10T10:04:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dinaiz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dinaiz/32/182_2.png) [@dinaiz](https://forum.juce.com/u/dinaiz)\
**Post date:** [February 10, 2010, 10:04am UTC](https://forum.juce.com/t/suggestion-microseconds-timer/4871/1 "2010-02-10T10:04:42Z")

</div>

Hi Jules,

I’ve seen that the timer classes in Juce can only go down to millisecond accuracy.  
In case you’re interested, I’ve written a cross-platform class which measures time using microseconds

Please feel free to reuse this code (it probably not good enough to be used “as is” in Juce though)

[code]#include “timer.h”

#ifdef \_WINDOWS  
#include \<windows.h\>

```
LARGE_INTEGER countsPerSec;
boost::uint_fast64_t countsPerMicrosec;
LARGE_INTEGER startTime;

 Timer::Timer()
{
	QueryPerformanceFrequency(&countsPerSec);
	countsPerMicrosec=1000000*countsPerSec.QuadPart;
	QueryPerformanceCounter(&startTime);
}

boost::uint_fast64_t Timer::getRunningTimeMicrosec(void)
{
	LARGE_INTEGER currentTime; 		
	QueryPerformanceCounter(&currentTime);
	return (boost::uint_fast64_t)((currentTime.QuadPart-startTime.QuadPart)/countsPerMicrosec) ;
}

void Timer::waitFor(boost::uint_fast64_t durationInMicrosec)
{
	LARGE_INTEGER callTime; 		
	QueryPerformanceCounter(&callTime);
	boost::uint_fast64_t callTimeInMicrosecs=(boost::uint_fast64_t)callTime.QuadPart/countsPerMicrosec;

	if(durationInMicrosec>1000) // if theres more than 1ms to wait, then we wait by steps of 1ms 
	{
		Sleep((DWORD)durationInMicrosec/1000); //integer division -> integer result
	}
	while(getRunningTimeMicrosec()<durationInMicrosec+callTimeInMicrosecs);
	
}

void Timer::waitUntil(boost::uint_fast64_t timeInMicrosec)
{
	LARGE_INTEGER callTime; 
	QueryPerformanceCounter(&callTime);
	boost::uint_fast64_t callTimeInMicrosecs=(boost::uint_fast64_t)callTime.QuadPart/countsPerMicrosec;
	boost::int_fast64_t deltaMicrosec=timeInMicrosec-callTimeInMicrosecs;
	if (deltaMicrosec<0)
	{
		return; //shit, we're already late !
	}
	else
	{
		if(deltaMicrosec>1000) // if theres more than 1ms to wait, then we wait by steps of 1ms 
		{
			Sleep((DWORD)deltaMicrosec/1000); //integer division -> integer result
		}
	while(getRunningTimeMicrosec()<timeInMicrosec);
	}
}

```

#else // !\_WINDOWS -\> We can safely use boost 🙂  
#include \<boost/date\_time/posix\_time/posix\_time.hpp\>  
#include \<boost/thread.hpp\>

```
 Timer::Timer()
{
	startTime=boost::posix_time::microsec_clock::local_time();
}

boost::uint_fast64_t Timer::getRunningTimeMicrosec(void)
{
	return (boost::uint_fast64_t)(boost::posix_time::microsec_clock::local_time()-startTime).total_microseconds();
}

void Timer::waitFor(boost::uint_fast64_t durationInMicrosec)
{
	boost::this_thread::sleep(boost::posix_time::microseconds(durationInMicrosec));	
}

void Timer::waitUntil(boost::uint_fast64_t timeInMicrosec)
{
	boost::uint_fast64_t callTime=getRunningTimeMicrosec();

	boost::int_fast64_t deltaMicrosec=timeInMicrosec-callTime;
	if (deltaMicrosec<0)
	{
		return; //shit, we're already late !
	}
	else
	{
		boost::this_thread::sleep(boost::posix_time::microseconds(deltaMicrosec));	
	}
}

```

#endif[/code]

It uses boost though …

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [February 10, 2010, 10:29am UTC](https://forum.juce.com/t/suggestion-microseconds-timer/4871/2 "2010-02-10T10:29:59Z")

</div>

What’s wrong with Time::getMillisecondCounterHiRes() or Time::getHighResolutionTicks() ?

---

<div class="post-metadata">

**Author:** ![dinaiz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dinaiz/32/182_2.png) [@dinaiz](https://forum.juce.com/u/dinaiz)\
**Post date:** [February 10, 2010, 10:54am UTC](https://forum.juce.com/t/suggestion-microseconds-timer/4871/3 "2010-02-10T10:54:48Z")

</div>

Errr …Time::getMillisecondCounterHiRes() returns milliseconds, not microseconds, or did I miss something ? 🙂

However, my implementation is absolutely not necessary at all, as you have everything needed (and better implemented 🙂 ) in juce\_win32\_SystemStats.cpp  
I guess the correct, cross platform way to get the time in microseconds is simply to make something like :

```auto

```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [February 10, 2010, 11:06am UTC](https://forum.juce.com/t/suggestion-microseconds-timer/4871/4 "2010-02-10T11:06:21Z")

</div>

> [@](#):
>
> Errr …Time::getMillisecondCounterHiRes() returns milliseconds, not microseconds, or did I miss something ? 🙂

It returns a _double_…

---

<div class="post-metadata">

**Author:** ![dinaiz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dinaiz/32/182_2.png) [@dinaiz](https://forum.juce.com/u/dinaiz)\
**Post date:** [February 10, 2010, 11:12am UTC](https://forum.juce.com/t/suggestion-microseconds-timer/4871/5 "2010-02-10T11:12:30Z")

</div>

> [@jules](#):
>
> [quote]Errr …Time::getMillisecondCounterHiRes() returns milliseconds, not microseconds, or did I miss something ? 🙂

It returns a _double_…[/quote]

This totally proves I should drink more cofee. sorry for making you loose your time :roll:
