国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

在C#中實(shí)現(xiàn)高性能計(jì)時(shí)

2024-07-21 02:21:11
字體:
供稿:網(wǎng)友
for performance test, it is very important to measure code execution time. without measurement, there is no way to tell if we meet performance goal.

system.environment.tickcount is not suitable for high resolution timing. its resolution cannot be less than 500 milliseconds.

system.datetime.now returns the current time of type datetime. with start datetime and end datetime, we can get the interval as a value of timespan by (end - start ) . timespan.totalmilliseconds or timespan.ticks may be used to read interval. from msdn, the resolution of system.datetime.now depends on the system timer.

system approximate resolution
windows nt 3.5 and later 10 milliseconds
windows 98 55 milliseconds

so it is better but not high resolution at all.


in .net framework v1 and v1.1, we have to use p/invoke to get high resolution reading. the class below is commonly used in performance test measurement. it is querying hardware to get high resolution performance counter. for more information (including what happens if the hardware does not support high resolution performance counter) please check msdn for queryperformancecounter and queryperformancefrequency.

public class highresolutiontimer
{
private long start;
private long stop;
private long frequency;

public highresolutiontimer()
{
queryperformancefrequency (ref frequency);
}

public void start ()
{
queryperformancecounter (ref start);
}

public void stop ()
{
queryperformancecounter (ref stop);
}

public float elapsedtime
{
get{
float elapsed = (((float)(stop - start)) / ((float) frequency));
return elapsed;
}
}

[system.runtime.interopservices.dllimport("kernel32.dll", charset=system.runtime.interopservices.charset.auto)]
private static extern bool queryperformancecounter( [in, out] ref long performancecount);
[system.runtime.interopservices.dllimport("kernel32.dll", charset=system.runtime.interopservices.charset.auto)]
private static extern bool queryperformancefrequency( [in, out] ref long frequency);
}

to illustrate the use of this class, check the code below.

highresolutiontimer timer = new highresolutiontimer();
timer.start();
//perf test
timer.stop();
console.writeline(timer.elapsedtime);

(this posting is provided "as is" with no warranties, and confers no rights. use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm)



發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 山阴县| 大安市| 封丘县| 武宣县| 兴安县| 南昌县| 德兴市| 互助| 尚志市| 彩票| 宁河县| 苍南县| 噶尔县| 嘉鱼县| 潍坊市| 德清县| 太仆寺旗| 东乌珠穆沁旗| 青龙| 嘉黎县| 广河县| 嘉鱼县| 宁强县| 怀来县| 诸城市| 信阳市| 和田县| 涿州市| 淅川县| 盐边县| 自治县| 轮台县| 米林县| 万年县| 修水县| 临漳县| 南康市| 米易县| 德州市| 米脂县| 兰州市|