国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 吉首市| 平定县| 利川市| 泰顺县| 巴马| 荣昌县| 建宁县| 雅江县| 湄潭县| 永福县| 东乡族自治县| 讷河市| 闽清县| 若尔盖县| 绥芬河市| 琼海市| 宕昌县| 德清县| 武城县| 含山县| 肥城市| 金昌市| 铜梁县| 鹤庆县| 西青区| 苍梧县| 高安市| 池州市| 临夏县| 崇仁县| 普宁市| 台南县| 济源市| 通州区| 高碑店市| 昭苏县| 达尔| 土默特左旗| 新宾| 玛多县| 陈巴尔虎旗|