Visual c++ 如何在visualc++;有一个简单的计时器,用于精确显示执行时间
我在程序中使用“time.h”表示从一个代码点到另一个代码点的执行时间:Visual c++ 如何在visualc++;有一个简单的计时器,用于精确显示执行时间,visual-c++,timer,Visual C++,Timer,我在程序中使用“time.h”表示从一个代码点到另一个代码点的执行时间: #include <time.h> ... //clock clock_t avvio; clock_t fine; //start measurement avvio = clock(); ..... do works .... //end measurement fine = clock()-avvi
#include <time.h>
...
//clock
clock_t avvio;
clock_t fine;
//start measurement
avvio = clock();
.....
do works
....
//end measurement
fine = clock()-avvio;
//conversion in seconds
double tempoimp = fine / (double)CLOCKS_PER_SEC;
#包括
...
//钟
时钟;
时钟不好;
//开始测量
avvio=clock();
.....
工作
....
//末端测量
精细=时钟()-avvio;
//秒转换
double tempoimp=每秒精细/(双)时钟;
我在这里找到了这个代码,并使用它。工作正常,但“经过”秒很好/1000,这假设是一个“硬实时”系统,而我的笔记本不是
那么,您建议我使用什么来度量一组指令所需的时间,这样我就可以得到程序的执行时间(以秒为单位)
我需要真正的毫秒。。。不是时钟周期的划分
有人可以帮我吗?你可以使用
clock\u gettime
,它提供了更高分辨率的计时器
struct timespec t1 = { 0, 0 },
t2 = { 0, 0 };
if (clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &t1) < 0){
perror ("Failed to get cpu time");
return -1;
}
/* test code starts here */
for (int i = 0; i < 200000000; i++); // hefty loop
/* test code ends here */
if (clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &t2) < 0){
perror ("Failed to get cpu time");
return -1;
}
printf ("Test code took %d.%d\n",
(int)(t2.tv_sec - t1.tv_sec),
(int)(t2.tv_nsec - t1.tv_nsec));
struct timespec t1={0,0},
t2={0,0};
if(时钟获取时间(时钟处理时间ID和t1)<0){
perror(“无法获取cpu时间”);
返回-1;
}
/*测试代码从这里开始*/
对于(int i=0;i<200000000;i++);//大环
/*测试代码到此结束*/
if(时钟获取时间(时钟处理时间ID和t2)<0){
perror(“无法获取cpu时间”);
返回-1;
}
printf(“测试代码花费了%d.%d\n”,
(内部)(t2.tv秒-t1.tv秒),
(int)(t2.tv_nsec-t1.tv_nsec));
但时钟周期将更准确地反映代码的执行速度。您无法使用任何时间测量来准确地测量程序速度。这是因为有很多变量可能会影响程序的执行时间:
- 正在运行的其他进程数
- 计算机上可用ram的数量
- 处理器时钟速度的变化
#include <stdio.h>
#include <windows.h>
int main (){
LARGE_INTEGER count1, count2, freq;
if (!QueryPerformanceCount (&count1)){
perror ("Couldn't get first count");
return -1;
}
for (int i = 0; i < 200000000; i++);
if (!QueryPerformanceCount (&count2)){
perror ("Couldn't get second count");
return -1;
}
if(!QueryPerformanceFrequency(&freq)){
perror ("Couldn't get processor frequency");
return -1;
}
#if ( __WORDSIZE == 64 )
#pragma message "Performing 64-bit build"
printf ("The difference is %ll\n", count2.QuadPart - count1.QuadPart);
printf ("Time (appx): %l\n", (count2.QuadPart - count1.QuadPart) / freq.QuadPart );
#else
#pragma message "Performing 32-bit build"
/* The code for 32-bit builds here is incomplete. The difference
* becomes inaccurate after the time has exceeded 32-bits. You can
* work out the math to compensate for that, or just start compiling
* for 64-bit builds. (Google "compile 64 bit visual studio").
*
*
*/
#endif
/* A processor frequency can change during a
* computationally intensive program. Therefore,
* the difference in clock ticks (above) is the most
* accurate way to measure performance.
*/
}
#包括
#包括
int main(){
大整数count1,count2,freq;
if(!QueryPerformanceCount(&count1)){
佩罗尔(“无法获得第一次计数”);
返回-1;
}
对于(int i=0;i<200000000;i++);
if(!QueryPerformanceCount(&count2)){
佩罗(“无法获得第二次计数”);
返回-1;
}
if(!QueryPerformanceFrequency(&freq)){
perror(“无法获取处理器频率”);
返回-1;
}
#if(uuu WORDSIZE==64)
#pragma消息“正在执行64位生成”
printf(“差值为%ll\n”,count2.QuadPart-count1.QuadPart);
printf(“时间(appx):%l\n”,(count2.QuadPart-count1.QuadPart)/freq.QuadPart);
#否则
#pragma消息“正在执行32位生成”
/*此处32位生成的代码不完整。差异
*时间超过32位后变得不准确。您可以
*算出数学来弥补这一点,或者直接开始编译
*用于64位版本。(谷歌“编译64位visual studio”)。
*
*
*/
#恩迪夫
/*处理器频率可能在运行期间发生变化
*计算密集型程序。因此,
*时钟滴答声(上图)的差异最大
*衡量绩效的准确方法。
*/
}
可能重复我在发布前搜索的问题…更相似的答案..但都具有相同的库和我发布的情况。相反,我要求建议我必须使用什么库或代码来获得实时(秒)表示……是的,应该是time.h
。是否出现编译错误?*更新了示例。在这个问题上,你可能需要和我合作一点,因为我没有一台windows计算机来测试这个问题。在声明中去掉=0
,那是不应该存在的。因为你有一台32位的计算机,我不小心写了count2.u.LowPart-count2.u.LowPart
。将其更改为count2.u.LowPart-count1.u.LowPart
不完全正确,您必须查询处理器频率。我马上更新我的答案