getUTCtime() increases irregularly

david tazz at gmx.at
Sat Apr 7 17:09:31 PDT 2007


Jarrett Billingsley schrieb:
> "Manfred Nowak" <svv1999 at hotmail.com> wrote in message 
> news:ev3jhs$28o4$1 at digitalmars.com...
>> Jarrett Billingsley wrote
>>
>>> Unfortunately if David is looking to use this as a timestamp
>> In the OP it says:
>>
>> | playing with a slow down class
>>
>> -manfred
> 
> I... don't know what that is. 
> 

Sorry, here's a draft of the class.
I'm sure there is a proper name for its funtionality,
I just don't know it.

david


import std.date : d_time, getUTCtime;

// todo: perhaps check in syncCycle if startCycle has been called before 
at all (costs how much time?)
/**
  * prevents a program from running too fast by mapping code to time 
intervals
  * note: timer increments are only in quantities of ~15 or ~16 
milliseconds,
  * inbetween these intervals there is no increment $(BR)
  * therefore, the timing can be off ~15 or ~16 milliseconds
  */
/** Example:
  * Assures that after code A is finished at least 100 milliseconds have 
passed before code C is executed
  * ---
  * Sync sync = new Sync;
  * ... some code A ...
  * sync.startCycle();
  * ... some code B ...
  * sync.syncCycle(100);
  * ... some code C ...
  * ---
  */
class Sync
{
	d_time startTime;
	d_time passedTime;

	/// set the current time as the reference point
	void startCycle()
	{
		startTime = getUTCtime();
	}
	
	/** wait until msecs milliseconds have passed
	 *  since startCycle() has been called last $(BR)
	 *  if msecs or more milliseconds have passed already, return immediately
	 */
	void syncCycle(d_time msecs)
	{
		passedTime = getUTCtime() - startTime;
		while (passedTime < msecs)		// todo: replace passedTime by expression 
"getUTC... - startT..."
			passedTime = getUTCtime() - startTime;
	}
}


More information about the Digitalmars-d-learn mailing list