Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Callbacks and Timers » TimerCallBack interval resolution
TimerCallBack interval resolution [message #58017] Wed, 19 January 2022 07:41 Go to next message
deep is currently offline  deep
Messages: 245
Registered: July 2011
Location: Bangalore
Experienced Member
Hi

I am experimenting with TimerCallBack. I want to set callback interval to say 25 ms.
To check this I have done small code sample. It is attached to this message.
I am measuring delay with GetTickCount(). Running this for 10 sec. And counting - Histogram.

My observation shows time aligns to 20 ms

Timer Delay -20
Time Count
20 320
21 160
22 5
23 2
28 1
37 1


Timer Delay -25
Time Count
28 1
30 1
34 1
40 102
41 142


Timer Delay -19
Time Count
19 2
20 331
21 147
22 5
23 1
24 1
31 1
60 1


Timer Delay -22
Time Count
22 3
23 2
24 5
40 116
41 113
43 1

Timer Delay -38
Time Count
40 114
41 131
55 1


#include <CtrlLib/CtrlLib.h>

using namespace Upp;

struct TimerCallBackVariation : TopWindow
{

	Vector<int> interval;
	dword prev, now;

	TimeCallback timer, process;
	int timerDelay, processDelay;
	LineEdit le1;
	String Result;

	void Timer()
	{
		now = GetTickCount();
		interval.Add ( now - prev );
		prev = now ;
	}


	void Process()
	{
		timer.Kill();

		Result << "Count - " << interval.GetCount() << "\n";
		Result << "Interval Vector - " << interval << "\n" ;

		Sort ( interval );

		Result << "Sorted - " << interval << "\n";

		VectorMap<int, int> hist;
		int j, k;

		for ( int i = 0; i < interval.GetCount(); i++ )
		{
			j = interval[i];
			k = hist.Find ( j );

			if ( k > -1 )
			{
				hist[k] = hist[k] + 1;
			}

			else
			{
				hist.Add ( j, 1 );
			}
		}
		
		Result << "\nTimer Delay " << timerDelay << "\n";

		Result << "Time  \tCount" "\n";

		for ( int i = 0; i < hist.GetCount(); i++ )
		{
			Result << hist.GetKey ( i ) << "  \t" << hist.GetValues() [i] << "\n";
		}

		this->Title ( "Done" );
		le1.Paste(Result.ToWString());
	}

	TimerCallBackVariation()
	{
		timerDelay = -38;
		processDelay = 10000;
		Title ( "Timer Callback Time Variation" ).Zoomable().Sizeable();
		Add(le1);
		le1.SizePos();
		prev = GetTickCount();
		timer.Set ( timerDelay, [=] {Timer();} );
		process.Set ( processDelay, [=] { Process();} );
	}

};

GUI_APP_MAIN
{
	TimerCallBackVariation app;
	app.Run();
}



Warm Regards

Deepak
Re: TimerCallBack interval resolution [message #58018 is a reply to message #58017] Wed, 19 January 2022 13:19 Go to previous messageGo to next message
Tom1
Messages: 1080
Registered: March 2007
Senior Contributor
Hi Deepak,

Are you running on Windows? If so, you can improve your timing to one millisecond resolution by adding this in your program:
#ifdef _WIN32
#include <MMSystem.h>

INITBLOCK{
	timeBeginPeriod(1);
}

EXITBLOCK{
	timeEndPeriod(1);
}
#endif

You may also want to use usecs() for measuring time at better than millisecond resolution.

Best regards,

Tom

[Updated on: Wed, 19 January 2022 13:22]

Report message to a moderator

Re: TimerCallBack interval resolution [message #58019 is a reply to message #58018] Wed, 19 January 2022 14:27 Go to previous messageGo to next message
deep is currently offline  deep
Messages: 245
Registered: July 2011
Location: Bangalore
Experienced Member
Hi Tom

Thanks for reply.

I want this on Linux.

I need consistency in TimerCallback. Actually consistancy is there but with multiple of 20 ms interval.

My observation is I get time resolution of 20 ms.

I need to have say every 30 ms. +/- 2 ms callback. Is it possible with TimerCallback ?


Warm Regards

Deepak
Re: TimerCallBack interval resolution [message #58020 is a reply to message #58019] Wed, 19 January 2022 14:40 Go to previous messageGo to next message
Tom1
Messages: 1080
Registered: March 2007
Senior Contributor
Hi,

I'm afraid I cannot help you there. You need someone with better Linux timing knowledge...

Best regards,

Tom
Re: TimerCallBack interval resolution [message #58028 is a reply to message #58020] Thu, 20 January 2022 21:43 Go to previous messageGo to next message
Didier is currently offline  Didier
Messages: 674
Registered: November 2008
Location: France
Contributor
Hello Deep,

You should look at RTIMING.
There is an example : upp/reference/Timing

Works on windows and linux: it should do the job Wink
Re: TimerCallBack interval resolution [message #58030 is a reply to message #58017] Fri, 21 January 2022 07:39 Go to previous messageGo to next message
deep is currently offline  deep
Messages: 245
Registered: July 2011
Location: Bangalore
Experienced Member
Hi Didier,

My issue is not about time profiling.

I am looking at time interval and repeatability of time interval.

When I set TimerCallback time value between -22 to -39 ms. I get interval of 40 ms.
Depending on value of time I set I get time interval in multiple of 20 ms.

If I want 30 ms. time interval for timercallback I am not getting it. I get greater time interval - multiple time interval of 20 ms.


Interval repeatability is 1ms. which is okay for me.


Warm Regards

Deepak
Re: TimerCallBack interval resolution [message #58036 is a reply to message #58030] Sat, 22 January 2022 19:32 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13623
Registered: November 2005
Ultimate Member
deep wrote on Fri, 21 January 2022 07:39
Hi Didier,

My issue is not about time profiling.

I am looking at time interval and repeatability of time interval.

When I set TimerCallback time value between -22 to -39 ms. I get interval of 40 ms.
Depending on value of time I set I get time interval in multiple of 20 ms.

If I want 30 ms. time interval for timercallback I am not getting it. I get greater time interval - multiple time interval of 20 ms.


Interval repeatability is 1ms. which is okay for me.


It is supposed to be about 20ms by design and it is not even guaranteed. If you need more precise timing, you need to run it in separate thread.

More details: GUI timer is run each time after the set of GUI events is processed and queue becomes empty. Those 20ms actually apply for the case that there are no events - if there are no event for 20 ms, timer is invoked. That also means if the GUI event (e.g. keystroke) takes more than 20ms to process, the interval can be actually much bigger than 20ms.
Re: TimerCallBack interval resolution [message #58046 is a reply to message #58036] Mon, 24 January 2022 09:56 Go to previous messageGo to next message
deep is currently offline  deep
Messages: 245
Registered: July 2011
Location: Bangalore
Experienced Member
Hi Mirek,

Thanks for explanation.

I was under impression that SetTimeCallback/TimeCallback as independent time trigger.

mirek wrote on Sun, 23 January 2022 00:02


It is supposed to be about 20ms by design and it is not even guaranteed. If you need more precise timing, you need to run it in separate thread.



Small code snippet or link will be helpful to setup repeat callback independent of GUI.


Warm Regards

Deepak
Re: TimerCallBack interval resolution [message #58047 is a reply to message #58046] Mon, 24 January 2022 10:47 Go to previous message
mirek is currently offline  mirek
Messages: 13623
Registered: November 2005
Ultimate Member
deep wrote on Mon, 24 January 2022 09:56
Hi Mirek,

Thanks for explanation.

I was under impression that SetTimeCallback/TimeCallback as independent time trigger.

mirek wrote on Sun, 23 January 2022 00:02


It is supposed to be about 20ms by design and it is not even guaranteed. If you need more precise timing, you need to run it in separate thread.



Small code snippet or link will be helpful to setup repeat callback independent of GUI.


Frankly, the only time I did that was in SDL2 based sound synthetiser and there the period is set by SDL2 sound system.

Still, you can check it here:

https://github.com/ultimatepp/ultimatepp/blob/master/example s/Synth/Core.cpp

(or in examples/SDLSoundDemo)
Previous Topic: how to do to stop progress zip file ?
Goto Forum:
  


Current Time: Thu May 26 22:00:24 CEST 2022

Total time taken to generate the page: 0.03464 seconds