Measuring Time More Accurately(vaultofthoughts.net)

submitted by mikeonmikeon(5200) 5 years, 7 months ago

How to accurately measure how much time has passed while performing some operation? Use the Stopwatch class. Here is how.

3 comments |category: |Views: 23

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by fatdavehfatdaveh(1420) 5 years, 7 months ago 0

Easy, handy, I like it. thanks

Reply

posted by superghostsuperghost(2260) 5 years, 7 months ago 0

This is exactly what I was looking for, even before I started looking for it ;) Great kick, thanks!

Reply

posted by sdhakshinasdhakshina(0) 4 years, 5 months ago 0

Hey Thanks All,

I tried Stopwatch .NET 2.0 but its not found, or i think i am missing some reference.
So i used the following program, and it worked fine.

using System;
using System.Runtime.InteropServices;
namespace TestSqlTimeOutException_Soundar
{
public sealed class StopWatch
{
[DllImport("kernel32.dll")]
extern static int QueryPerformanceCounter(ref long x);

[DllImport("kernel32.dll")]
extern static int QueryPerformanceFrequency(ref long x);


public StopWatch()
{
Frequency = GetFrequency();
Reset();
}

public void Reset()
{
StartTime = GetValue();
}

public long Peek()
{
return (long)(((GetValue() - StartTime)
/ (double)Frequency) * 10000);
}

private long GetValue()
{
long ret = 0;
if (QueryPerformanceCounter(ref ret) == 0)
throw new NotSupportedException(
"Error while querying "
+ "the high-resolution performance counter.");
return ret;
}

private long GetFrequency()
{
long ret = 0;
if (QueryPerformanceFrequency(ref ret) == 0)
throw new NotSupportedException(
"Error while querying "
+ "the performance counter frequency.");
return ret;
}

private long StartTime
{
get
{
return m_StartTime;
}
set
{
m_StartTime = value;
}
}

private long Frequency
{
get
{
return m_Frequency;
}
set
{
m_Frequency = value;
}
}



#region -- Private Variables --

/// <summary>
/// Holds the value of the StartTime property.
/// </summary>
private long m_StartTime;

/// <summary>
/// Holds the value of the Frequency property.
/// </summary>
private long m_Frequency;

#endregion

}
}

Reply

information Login or create an account to comment on this story