如何以毫秒为单位测量性能? [英] How to measure performance by milliseconds?

查看:81
本文介绍了如何以毫秒为单位测量性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要测试我的一些代码的性能。

我尝试使用Environment.TickCount但其分辨率太低(来自

MSDN备注: TickCount属性的分辨率不能低于500毫秒。),我也试过了DateTime,它也给了我最低分辨率为500毫秒的


但是我需要测量的时间少于500毫秒,我需要它几个毫秒才能获得


我该怎么做?


--------

谢谢

Sharon

I need to test a performance of some code of mine.
I tried using the Environment.TickCount but its resolution is too low (from
the MSDN remarks: The resolution of the TickCount property cannot be less
than 500 milliseconds.), I also tried the DateTime and it also gives me
minimum resolution of 500 milliseconds.
But I need to measure much less the 500 milliseconds, I need it to be
several milliseconds.
How can I do that?

--------
Thanks
Sharon

推荐答案

您好,

QueryPerformanceFrequency和QueryPerformanceCounter是您正在寻找的方法

。请注意,两者都是Win32方法,你需要

来对它们进行排序。您将在 www.pinvoke.net 上找到签名。
http://www.pinvoke.net/default.aspx/...ceCounter .html


=?Utf-8?B?U2hhcm9u?=< Sh ***** @ newsgroups.nospam>写在

新闻:B9 ********************************** @ microsof t .com:
Hi,
QueryPerformanceFrequency and QueryPerformanceCounter are the methods
you are looking for. Note that both are Win32 method and you will need
to pinvoke them. you will find the signatures on www.pinvoke.net.
http://www.pinvoke.net/default.aspx/...ceCounter.html

=?Utf-8?B?U2hhcm9u?= <Sh*****@newsgroups.nospam> wrote in
news:B9**********************************@microsof t.com:
我需要测试我的一些代码的性能。
我尝试使用Environment.TickCount但其分辨率太低
(来自MSDN备注:TickCount属性的分辨率不能小于500毫秒。),我也试过了DateTime,它也给了我500毫秒的最小分辨率。
但是我需要为了测量少于500毫秒,我需要它几毫秒。
我该怎么做?

--------
谢谢
Sharon
I need to test a performance of some code of mine.
I tried using the Environment.TickCount but its resolution is too low
(from the MSDN remarks: The resolution of the TickCount property
cannot be less than 500 milliseconds.), I also tried the DateTime and
it also gives me minimum resolution of 500 milliseconds.
But I need to measure much less the 500 milliseconds, I need it to be
several milliseconds.
How can I do that?

--------
Thanks
Sharon






以下是我使用的一些包装QueryPerformanceCounter API的代码:

using系统;

使用System.Runtime.InteropServices;

使用System.ComponentModel;

使用System.Threading;

名称空间PAB

{

公共类HiPerfTimer

{

[DllImport(" Kernel32.dll")]

private static extern bool QueryPerformanceCounter(out out

lpPerformanceCount);

[DllImport(" ; Kernel32.dll")]

private static extern bool QueryPerformanceFrequency(out long lpFrequency);

private long startTime;

private long stopTime;

私人长频;

///< summary>

/// ctor

/// < / summary>

public HiPerfTimer()

{

startTime = 0;

stopTime = 0;

freq = 0;

if(QueryPerformanceFrequency(out freq)== false)

{

throw new Win32Exception(); //不支持计时器

}

}

///< summary>

///开始计时器

///< / summary>

///< returns> long - tick count< / returns>

public long Start()

{

QueryPerformanceCounter(out startTime);

返回startTime;

}

///< summary>

///停止计时器

///< / summary>

/ //< returns> long - tick count< / returns>

public long Stop()

{

QueryPerformanceCounter(out stopTime);

返回停止时间;

}

///< summary>

///返回持续时间计时器(以秒为单位)

///< / summary>

///< returns> double - duration< / returns>

public double Duration

{

get

{

return(double)(stopTime - startTime)/ (双)频率;

}

}

/// < summary>

///计时器的频率(此机器上一秒钟没有计数)

///< / summary>

///< returns> long - 频率< / return>

公共长频率

{

get

{

QueryPerformanceFrequency(out freq);

返回频率;

}

}

}

}

-

联合创始人,Eggheadcafe.com开发者门户网站:
http://www.eggheadcafe.com

UnBlog:
http://petesbloggerama.blogspot.com


" Sharon"写道:
Here is some code I use that wraps the QueryPerformanceCounter API:
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Threading;
namespace PAB
{
public class HiPerfTimer
{
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long
lpPerformanceCount);
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(out long lpFrequency);
private long startTime ;
private long stopTime;
private long freq;
/// <summary>
/// ctor
/// </summary>
public HiPerfTimer()
{
startTime = 0;
stopTime = 0;
freq =0;
if (QueryPerformanceFrequency(out freq) == false)
{
throw new Win32Exception(); // timer not supported
}
}
/// <summary>
/// Start the timer
/// </summary>
/// <returns>long - tick count</returns>
public long Start()
{
QueryPerformanceCounter(out startTime);
return startTime;
}
/// <summary>
/// Stop timer
/// </summary>
/// <returns>long - tick count</returns>
public long Stop()
{
QueryPerformanceCounter(out stopTime);
return stopTime;
}
/// <summary>
/// Return the duration of the timer (in seconds)
/// </summary>
/// <returns>double - duration</returns>
public double Duration
{
get
{
return (double)(stopTime - startTime) / (double) freq;
}
}
/// <summary>
/// Frequency of timer (no counts in one second on this machine)
/// </summary>
///<returns>long - Frequency</returns>
public long Frequency
{
get
{
QueryPerformanceFrequency(out freq);
return freq;
}
}
}
}
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Sharon" wrote:
我需要测试我的一些代码的性能。
我尝试使用Environment.TickCount但其分辨率太低(来自
MSDN评论:TickCount属性的分辨率不能低于500毫秒。),我也试过了DateTime,它也给了我最低500毫秒的分辨率。
但是我需要测量少于500毫秒,我需要它几毫秒。
我该怎么做?

--------
谢谢
Sharon
I need to test a performance of some code of mine.
I tried using the Environment.TickCount but its resolution is too low (from
the MSDN remarks: The resolution of the TickCount property cannot be less
than 500 milliseconds.), I also tried the DateTime and it also gives me
minimum resolution of 500 milliseconds.
But I need to measure much less the 500 milliseconds, I need it to be
several milliseconds.
How can I do that?

--------
Thanks
Sharon



Think DateTime.Now准确度为10ms。不确定这对你的

应用程序来说是否太多了。此外,这个链接似乎表明Environment.TickCount是1ms准确

基于文章的解释和经验证据。不确定。
http://www.informit.com/guides/print...eqNum=241&rl=1

-

William Stacey [MVP]


" Sharon" <嘘***** @ newsgroups.nospam>在留言中写道

新闻:B9 ********************************** @ microsof t.com ...

|我需要测试一些我的代码的性能。

|我尝试使用Environment.TickCount,但其分辨率太低

(来自

| MSDN备注:TickCount属性的分辨率不能低于

|超过500毫秒。),我也尝试了DateTime,它也给了我

|最低分辨率为500毫秒。

|但是我需要测量的时间少于500毫秒,我需要的是

|几毫秒。

|我怎么能这样做?

|

| --------

|谢谢

|沙龙
Think DateTime.Now is 10ms accurate. Not sure if that is too much for your
app. Also, this link seems to suggest Environment.TickCount is 1ms accurate
based on interpretation of the article and empirical evidence. Not sure.
http://www.informit.com/guides/print...eqNum=241&rl=1

--
William Stacey [MVP]

"Sharon" <Sh*****@newsgroups.nospam> wrote in message
news:B9**********************************@microsof t.com...
|I need to test a performance of some code of mine.
| I tried using the Environment.TickCount but its resolution is too low
(from
| the MSDN remarks: The resolution of the TickCount property cannot be less
| than 500 milliseconds.), I also tried the DateTime and it also gives me
| minimum resolution of 500 milliseconds.
| But I need to measure much less the 500 milliseconds, I need it to be
| several milliseconds.
| How can I do that?
|
| --------
| Thanks
| Sharon


这篇关于如何以毫秒为单位测量性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆