计算一段代码运行的时间?这么是否到了毫秒级别

计算一段代码运行的时间?这样是否到了毫秒级别?



#pragma once
/*
//计算代码段运行时间的类
//
*/
#include <iostream>

#ifndef ComputeTime_h
#define ComputeTime_h


class   ComputeTime    
{  
private:  
int Initialized;  
__int64 Frequency;  
__int64 BeginTime;  
    
public:  

bool Avaliable();  
double End();  
bool Begin();  
ComputeTime();  
virtual   ~ComputeTime();    

};  


#endif




#include "stdafx.h"
#include "ComputeTime.h"
#include <iostream>
#include <Windows.h>

ComputeTime::ComputeTime()  
{  
Initialized=QueryPerformanceFrequency((LARGE_INTEGER   *)&Frequency);  
}  
   
 ComputeTime::~ComputeTime()  
{  
    
}  
   
 bool   ComputeTime::Begin()  
{  
if(!Initialized)  
return 0;

 return   QueryPerformanceCounter((LARGE_INTEGER   *)&BeginTime);  
 }
     
 double   ComputeTime::End()
{  
 if(!Initialized)  
return 0;

   
 __int64   endtime;  
   
 QueryPerformanceCounter((LARGE_INTEGER   *)&endtime);  
    
  
 __int64   elapsed = endtime-BeginTime;  
    
  
 return   ((double)elapsed/(double)Frequency)*1000.0;  //单位毫秒
 }  

 bool   ComputeTime::Avaliable()
{  
 return Initialized;  
}   


------解决方案--------------------
看了下代码没问题,就是毫秒级的
------解决方案--------------------
"return   ((double)elapsed/(double)Frequency)*1000.0;  //单位毫秒"----是毫秒级的。
有个函数很关键     BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);
  BOOL QueryPerformanceCounter (LARGE_INTEGER *lpCount);
数据类型LARGEINTEGER既可以是一个作为8字节长的整数,也可以是作为两个4字节长的整数的联合结构,其具体用法根据编译器是否支持64位而定。该类型的定义如下:
  typeef union _ LARGE_INTEGER
  {
   struct
   {
   DWORD LowPart;
   LONG HighPart;
   };
   LONGLONG QuadPart;
  } LARGE_INTEGER;
在定时前应该先调用QueryPerformanceFrequency()函数获得机器内部计时器的时钟频率。接着在需要严格计时的事件发生前和发生之后分别调用QueryPerformanceCounter(),利用两次获得的计数之差和时钟频率,就可以计算出事件经历的精确时间。测试函数SLEEP(100)的精确持续时间方法:

  LARGE_INTEGER litmp;
  LONGLONG qt1,qt2;
  double dft,dff,dfm;
  QueryPerformanceFrequency(&litmp);//获得时钟频率
  dff=(double)litmp.QuadPart;
  QueryPerformanceCounter(&litmp);//获得初始值
  qt1=litmp.QuadPart;Sleep(100);
  QueryPerformanceCounter(&litmp);//获得终止值
  qt2=litmp.QuadPart;
  dfm=(double)(qt2-qt1);
  dft=dfm/dff;//获得对应的时间值计算一段代码运行的时间?这么是否到了毫秒级别
------解决方案--------------------
是毫秒级别的,不过有更简单点的方法,加个头文件time.h,然后int s,e;s = clock();e = clock();,然后用e-s得到的就是单位为毫秒的时间了
------解决方案--------------------
接收学习中。。。
------解决方案--------------------
GetSystemTimeAsFileTime
The GetSystemTimeAsFileTime function obtains the current system date and time. The information is in Coordinated Universal Time (UTC) format.

VOID GetSystemTimeAsFileTime(
  LPFILETIME lpSystemTimeAsFileTime   // pointer to a file time 
                                      // structure
);
 
Parameters
lpSystemTimeAsFileTime 
Pointer to a FILETIME structure to receive the current system date and time in UTC format. 
Return Values
This function does not return a value. 

Remarks
The GetSystemTimeAsFileTime function is equivalent to the following code sequence:

FILETIME ft;
SYSTEMTIME st;
GetSystemTime(&st);
SystemTimeToFileTime(&st,&ft);
 
QuickInfo
  Windows NT: Requires version 3.5 or later.
  Windows: Requires Windows 95 or later.
  Windows CE: Unsupported.
  Header: Declared in winbase.h.
  Import Library: Use kernel32.lib.

See Also
Time Overview, Time Functions, FILETIME, GetSystemTime, SYSTEMTIME, SystemTimeToFileTime