TheSwamp

Code Red => .NET => Topic started by: MickD on June 22, 2006, 03:23:40 AM

Title: Function timer
Post by: MickD on June 22, 2006, 03:23:40 AM
Here's a very quick knock up of a function timer, very basic and I put it here for improvement and discussion. Just add this class in your project somewhere and away you go.

Code: [Select]
/// <summary>
/// //Class to time the execution of functions for testing and comparisons
/// </summary>
public class FunctionTimer
{
public FunctionTimer()
{
//
// TODO: Add constructor logic here
//
}
private double _startTicks;
private double _endTicks;
private double _totalTime;
public void Start()
{
_startTicks = Convert.ToDouble(DateTime.Now.Ticks);
}
public void Stop()
{
_endTicks = Convert.ToDouble(DateTime.Now.Ticks);
_totalTime = (_endTicks - _startTicks)/10000000.0;
}

public string ElapsedTime
{
get
{
return "Total Elapsed Time = " + _totalTime.ToString() + " seconds\n";
}
}

An example in acad -

Code: [Select]
[CommandMethod("TIMERTEST")]
public static void TestTimer()
{
FunctionTimer timer = new FunctionTimer();
timer.Start();
Editor ed =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
int i = 0;
int j = 1;
int k = 2;
for(int a = 0; a < 1000; a++)
{
ed.WriteMessage("\nTesting {0} {1} {2}:",i += 3,j += 3,k += 3);
}
timer.Stop();
ed.WriteMessage(timer.ElapsedTime);
}

enjoy!
Title: Re: Function timer
Post by: Bobby C. Jones on June 23, 2006, 03:26:58 PM
Here's a very quick knock up of a function timer, very basic and I put it here for improvement and discussion.

Hey Mick,
You may find this article and code quite interesting.  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/scalenethowto09.asp
Title: Re: Function timer
Post by: Kerry on June 23, 2006, 10:45:42 PM

Hey Mick,
You may find this article and code quite interesting.   ... msdn ...  (http://... msdn ...)

Interestingly enough, I bookmarked that article earlier in the week ..

.. I wonder if there is an established information study path that people follow :-)
Title: Re: Function timer
Post by: MickD on June 23, 2006, 10:52:47 PM


Hey Mick,
You may find this article and code quite interesting.   ... msdn ...  (http://... msdn ...)
Thanks Bobby, I'll wack that in there as well just for comparison, see if there is any major difference.
I was wondering how you could test the accuracy of the simple timer but I use it mainly for testing db connection/updates etc where the actual time isn't as important just for comparisons.
.. I wonder if there is an established information study path that people follow :-)
It seems like that sometimes :)
Title: Re: Function timer
Post by: MickD on June 24, 2006, 10:37:34 PM
Ok, the results are in:

Without debugging:
Quote
Started Timer:
Iterations: 5
Average time per iteration:
0.998617924903864 seconds
998.617924903864 milliseconds
998617924.903864 nanoseconds

Duration of test run:
5 seconds
5000 milliseconds
QPC's Timer result: 4.99308962451932 seconds
MickD's FunctionTimer result: Total Elapsed Time = 5 seconds

While debugging:
Quote
Started Timer:
Iterations: 5
Average time per iteration:
0.994149200526882 seconds
994.149200526883 milliseconds
994149200.526883 nanoseconds

Duration of test run:
5 seconds
5000 milliseconds
QPC's Timer result: 4.97074600263441 seconds
MickD's FunctionTimer result: Total Elapsed Time = 5.015616 seconds

And the code complete, ready to paste into a new console app:
Code: [Select]
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace TimerTests
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
TestTimers();
}
public static void TestTimers()
{
FunctionTimer timer = new FunctionTimer();
QueryPerfCounter qpc = new QueryPerfCounter();
//jit the timers:
qpc.Start();
timer.Start();
qpc.Stop();
timer.Stop();
Console.WriteLine("Started Timer:");
// Time the overall test duration:
DateTime dtStartTime = DateTime.Now;

int i = 0;
int j = 1;
int k = 2;
int iterations = 5;
for(int a = 0; a < iterations; a++)
{
System.Threading.Thread.Sleep(1000);
}
qpc.Stop();
timer.Stop();

double result = qpc.Duration(iterations);
// Show the average time per iteration results
Console.WriteLine("Iterations: {0}", iterations);
Console.WriteLine("Average time per iteration: ");
Console.WriteLine(result/1000000000 + " seconds");
Console.WriteLine(result/1000000 + " milliseconds");
Console.WriteLine(result + " nanoseconds");

// Show the overall test duration results
DateTime dtEndTime = DateTime.Now;
Double duration = ((TimeSpan)(dtEndTime-dtStartTime)).TotalMilliseconds;
Console.WriteLine();
Console.WriteLine("Duration of test run: ");
Console.WriteLine(duration/1000 + " seconds");
Console.WriteLine(duration + " milliseconds");
Console.WriteLine("QPC's Timer result: {0}",(result/1000000000)*iterations + " seconds");
Console.WriteLine("MickD's FunctionTimer result: {0}",timer.ElapsedTime);
Console.ReadLine();
}
public class QueryPerfCounter
{
[DllImport("KERNEL32")]
private static extern bool QueryPerformanceCounter(
out long lpPerformanceCount);

[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(out long lpFrequency);

private long start;
private long stop;
private long frequency;
Decimal multiplier = new Decimal(1.0e9);

public QueryPerfCounter()
{
if (QueryPerformanceFrequency(out frequency) == false)
{
// Frequency not supported
throw new Win32Exception();
}
}

public void Start()
{
QueryPerformanceCounter(out start);
}

public void Stop()
{
QueryPerformanceCounter(out stop);
}

public double Duration(int iterations)
{
return ((((double)(stop - start)* (double) multiplier) / (double) frequency)/iterations);
}
}
/// <summary>
/// //Class to time the execution of functions for testing and comparisons
/// </summary>
public class FunctionTimer
{
public FunctionTimer()
{
//
// TODO: Add constructor logic here
//
}
private double _startTicks;
private double _endTicks;
private double _totalTime;
public void Start()
{
_startTicks = Convert.ToDouble(DateTime.Now.Ticks);
}
public void Stop()
{
_endTicks = Convert.ToDouble(DateTime.Now.Ticks);
_totalTime = (_endTicks - _startTicks)/10000000;
}

public string ElapsedTime
{
get
{
return "Total Elapsed Time = " + _totalTime.ToString() + " seconds\n";
}
}
}
}
}

That's close enough for me :)