CFLib.org – Common Function Library Project

makeStopwatch()

Last updated December 22, 2013

author

Adam Cameron

Version: 1 | Requires: CF10 | Library: UtilityLib

Description:
Uses closure to maintain a timeline which start() / lap() / stop() functions can capture points in time to, with an annotation.

Return Values:
A struct containing functions start(), lap(), stop()

Example:

stopwatch = makeStopwatch();

stopwatch.start("Begin timing");
sleep(500);
stopwatch.lap();
sleep(1500);
secondLap = stopwatch.lap("after another 1500ms");
writeDump(var=secondLap, label="secondLap");
sleep(2000);
stopwatch.start("Stop timing");
writeDump(var=stopWatch.getTimeline());

Parameters:

No arguments.

Full UDF Source:

/**
 * Returns functions for recording a sequence of timed events
 * v1.0 by Adam Cameron
 * 
 * @return A struct containing functions start(), lap(), stop() 
 * @author Adam Cameron (dac.cfml@gmail.com) 
 * @version 1.0, December 22, 2013 
 */
struct function makeStopwatch(){
	var timeline		= [];

	var lap = function(string message=""){
		var ticksNow	= getTickCount();
		var lapCount	= arrayLen(timeline);
		var lap			= {
			currentClock	= ticksNow,
			lapDuration		= lapCount > 0 ? ticksNow - timeLine[lapCount].currentClock : 0,
			totalDuration	= lapCount > 0 ? ticksNow - timeLine[1].currentClock : 0,
			message			= message
		};
		arrayAppend(timeline, lap);
		return lap;
	};

	return {
		start		= function(string message="start"){
			return lap(message);
		},
		lap			= function(string message="lap"){
			return lap(message);
		},
		stop		= function(string message="stop"){
			return lap(message);
		},
		getTimeline	= function(){
			return timeLine;
		}
	};
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson