用于统计函数执行时间的类
2007-08-20 20:15
375 查看
用于统计函数执行时间的类是最近在改进程序性能时写的,在函数入口处调用Start,在结束的时候调用Stop,在程序推出前调用Total进行统计输出。
该类不支持.net 1.1
using System;
using System.Data;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
namespace Common
{
/**//// <summary>
/// 函数执行时间统计
/// </summary>
/// <example>
/// public void function1()
/// {
/// QueryTime.Start("function1");
/// // 处理代码
/// QueryTime.Stop("function1");
/// }
///
/// public void function2()
/// {
/// QueryTime.Start("function2");
/// // 处理代码
/// QueryTime.Stop("function2");
/// }
///
/// public void main()
/// {
/// function1();
/// function2();
/// function1();
/// Console.WriteLine(QueryTime.Total());
/// }
///
/// result:
/// function1 Call 2 count, useTime 00:00:01.1234567
/// function2 Call 1 count, useTime 00:00:00.1234567
/// </example>
public class QueryTime
{
internal List<TimeSpan> timeSpan = new List<TimeSpan>();
internal Stopwatch watch = new Stopwatch();
public void Start(string key)
{
QueryTime qt;
if (!map.TryGetValue(key, out qt))
{
qt = new QueryTime();
map.Add(key, qt);
}
qt.watch.Reset();
qt.watch.Start();
}
public void Stop(string key)
{
QueryTime qt = map[key];
qt.watch.Stop();
qt.timeSpan.Add(qt.watch.Elapsed);
}
public void Reset(string key)
{
QueryTime qt = map[key];
qt.watch.Stop();
qt.watch.Reset();
}
Dictionary<string, QueryTime> map = new Dictionary<string, QueryTime>();
public string Total()
{
StringBuilder builder = new StringBuilder();
foreach (string key in map.Keys)
{
QueryTime qt = map[key];
TimeSpan total = new TimeSpan();
foreach (TimeSpan ts in qt.timeSpan)
{
total = total.Add(ts);
}
builder.AppendFormat("{0} Call {1} count, useTime {2} ", key, qt.timeSpan.Count, total);
}
return builder.ToString();
}
}
}
该类不支持.net 1.1
using System;
using System.Data;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
namespace Common
{
/**//// <summary>
/// 函数执行时间统计
/// </summary>
/// <example>
/// public void function1()
/// {
/// QueryTime.Start("function1");
/// // 处理代码
/// QueryTime.Stop("function1");
/// }
///
/// public void function2()
/// {
/// QueryTime.Start("function2");
/// // 处理代码
/// QueryTime.Stop("function2");
/// }
///
/// public void main()
/// {
/// function1();
/// function2();
/// function1();
/// Console.WriteLine(QueryTime.Total());
/// }
///
/// result:
/// function1 Call 2 count, useTime 00:00:01.1234567
/// function2 Call 1 count, useTime 00:00:00.1234567
/// </example>
public class QueryTime
{
internal List<TimeSpan> timeSpan = new List<TimeSpan>();
internal Stopwatch watch = new Stopwatch();
public void Start(string key)
{
QueryTime qt;
if (!map.TryGetValue(key, out qt))
{
qt = new QueryTime();
map.Add(key, qt);
}
qt.watch.Reset();
qt.watch.Start();
}
public void Stop(string key)
{
QueryTime qt = map[key];
qt.watch.Stop();
qt.timeSpan.Add(qt.watch.Elapsed);
}
public void Reset(string key)
{
QueryTime qt = map[key];
qt.watch.Stop();
qt.watch.Reset();
}
Dictionary<string, QueryTime> map = new Dictionary<string, QueryTime>();
public string Total()
{
StringBuilder builder = new StringBuilder();
foreach (string key in map.Keys)
{
QueryTime qt = map[key];
TimeSpan total = new TimeSpan();
foreach (TimeSpan ts in qt.timeSpan)
{
total = total.Add(ts);
}
builder.AppendFormat("{0} Call {1} count, useTime {2} ", key, qt.timeSpan.Count, total);
}
return builder.ToString();
}
}
}
相关文章推荐
- 用于统计函数执行时间的类
- Linux下的函数执行时间的统计方法
- c语言中如何添加计时函数,统计程序执行的时间
- Linux下的函数执行时间的统计方法
- 详解JS中统计函数执行次数与执行时间
- Linux下的函数执行时间的统计方法
- c语言中如何添加计时函数,统计程序执行的时间
- Linux下的函数执行时间的统计方法
- c语言中如何添加计时函数,统计程序执行的时间
- c语言中如何添加计时函数,统计程序执行的时间
- c语言中如何添加计时函数,统计程序执行的时间
- c语言中如何添加计时函数,统计程序执行的时间
- GPROF 统计函数执行时间
- golang 之 defer(统计函数执行时间)
- golang 之 defer(统计函数执行时间)
- c语言中如何添加计时函数,统计程序执行的时间
- Linux下的函数执行时间的统计方法
- linux 函数执行时间统计
- c语言中如何添加计时函数,统计程序执行的时间
- 在JS中统计函数执行次数与执行时间