Click here to Skip to main content
15,891,704 members
Articles / Programming Languages / C#

The List Trifecta, Part 1

Rate me:
Please Sign up or sign in to vote.
4.97/5 (21 votes)
20 May 2016LGPL321 min read 37.1K   161   40  
The A-list is an all-purpose list, a data structure that can support most standard list operation in O(log n) time and does lots of other stuff, too
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace Loyc.Runtime
{
	/// <summary>
	/// A timer class with a more convenient interface than 
	/// System.Diagnostics.Stopwatch. Its resolution is typically 10 ms. It 
	/// uses DateTime.UtcNow, so it could change suddenly and even become 
	/// negative if the user changes the system time, so be careful how you 
	/// rely on it.
	/// </summary>
	/// <remarks>
	/// With SimpleTimer, the timer starts when you construct the object and 
	/// it is always counting. You can get the elapsed time and restart the 
	/// timer from zero with a single call to Restart(). The Stopwatch class 
	/// requires you to make three separate method calls to do the same thing:
	/// you have to call ElapsedMilliseconds, then Reset(), then Start().
	/// </remarks>
	public class SimpleTimer
	{
		DateTime _start = DateTime.UtcNow;

		/// <summary>
		/// The getter returns the number of milliseconds since the timer was 
		/// started; the resolution of this property depends on the system timer.
		/// The setter changes the value of the timer.
		/// </summary>
		public int Millisec
		{
			get { return (int)((DateTime.UtcNow - _start).Ticks / 10000); }
			set {
				_start = new DateTime(DateTime.UtcNow.Ticks - (long)value * 10000);
			}
		}

		/// <summary>Restarts the timer from zero, and returns the number of 
		/// elapsed milliseconds prior to the reset.</summary>
		public int Restart()
		{
			DateTime now = DateTime.UtcNow;
			int millisec = (int)((now - _start).Ticks / 10000);
			_start = now;
			return millisec;
		}

		/// <summary>Restarts the timer from zero if , and returns the number of 
		/// elapsed milliseconds at the time of the restart.</summary>
		/// <returns>If the timer was restarted, this method returns the number of 
		/// elapsed milliseconds prior to the reset. Returns 0 if the timer was not 
		/// reset.</returns>
		public int RestartAfter(int minimumMillisec)
		{
			if ((uint)Millisec < (uint)minimumMillisec)
				return 0;
			else
				return Restart();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer None
Canada Canada
Since I started programming when I was 11, I wrote the SNES emulator "SNEqr", the FastNav mapping component, the Enhanced C# programming language (in progress), the parser generator LLLPG, and LES, a syntax to help you start building programming languages, DSLs or build systems.

My overall focus is on the Language of your choice (Loyc) initiative, which is about investigating ways to improve interoperability between programming languages and putting more power in the hands of developers. I'm also seeking employment.

Comments and Discussions