Last time I published a component that writes custom debug information (mainly time) in a record file, but because of the limitations of the CLR, it can only be accurate to about 10 milliseconds. I later referred to an article on the Web: http://blog.joycode.com/lostinet/archive/2005/04/24/49590.aspx (Thanks to the original author here first) gets a time record that can be accurate to about 1 milliseconds by invoking the system API. So I rewrote this component in C # to share with you.
//====================================================================
Tracespy-To write custom debug information in a record file (developer: Lin Jian)
//====================================================================
//
Property:
TraceFileName-Record file name
//
Method:
★ Text Writing Aspect
WRITETEXT-Write custom text
Clearalltext-Clear All text
★ Time Recording Aspect
Settimepoint-Set Time start
Gettimespanfrominit-Inquiry time span (distance time start)
Gettimespanfromprev-Query time span (distance from last inquiry)
★ Custom Counters
Setcounter-Set up custom counters
AddCounter-Add Custom counter
//
//====================================================================
Using System;
Namespace Tracespy
{
public class Thetrace
{
Record file name
static public string tracefilename = "Trace.txt";
Time start (initial for current time)
static private Long Inittimepoint = Timecounter.getexactnow (). Ticks;
Last query point (initial current time)
static private Long Prevtimepoint = Timecounter.getexactnow (). Ticks;
Custom Counters
static private int counter = 0;
Writing custom text
static public void WriteText (String str)
{
WRITETEXT (str, FALSE);
}
Ask time span (distance time start)
static public decimal gettimespanfrominit ()
{
Return Gettimespanfrominit ("");
}
static public decimal Gettimespanfrominit (string note)
{
Prevtimepoint = Timecounter.getexactnow (). Ticks;
Decimal span;
span = (decimal) (Prevtimepoint-inittimepoint)/(decimal) 10000;
Filewriter.writetext ("Ask the time span [" + Note + "], distance from the beginning of time" + span. ToString () + "Ms. ", false);
return span;
}
Ask time span (distance from last inquiry)
static public decimal Gettimespanfromprev ()
{
Return Gettimespanfromprev ("");
}
static public decimal Gettimespanfromprev (string note)
{
Long Recttimepoint =timecounter.getexactnow (). Ticks;
Decimal span;
span = (decimal) (Recttimepoint-prevtimepoint)/(decimal) 10000;
Prevtimepoint = Recttimepoint;
Filewriter.writetext ("Ask the time span [+ Note +]"], distance from the last Question Time "+ span. ToString () + "Ms. ", false);
return span;
}
Set up custom Counters
static public int Setcounter ()
{
Return Setcounter (0);
}
static public int setcounter (int num)
{
counter = num;
Filewriter.writetext (The custom counter value is set to + Counter +). ", false);
return counter;
}
Add Custom Counter
static public int AddCounter ()
{
return AddCounter (1);
}
static public int addcounter (int num)
{
Counter + = num;
Filewriter.writetext ("Add custom counter values to + Counter +".) ", false);
return counter;
}
}
}
Using System;
Using System.Runtime.InteropServices;
Namespace Tracespy
{
Internal class Timecounter
{
[DllImport ("kernel32.dll")]
static extern bool QueryPerformanceCounter ([in, out] ref long Lpperformancecount);
[DllImport ("kernel32.dll")]
static extern bool QueryPerformanceFrequency ([in, out] ref long lpfrequency);
static long _f = 0;
Static private Long GetTickCount ()
{
Long F = _f;
if (f = = 0)
{
if (QueryPerformanceFrequency (ref f))
{
_f = f;
}
Else
{
_f =-1;
}
}
if (f = = 1)
{
return Environment.tickcount * 10000;
}
Long c = 0;
QueryPerformanceCounter (ref c);
Return (long) ((double) c) * 1000 * 10000/((double) f));
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.