GetTickCount() 函数的作用和用法

DWORD GetTickCount(void);   

1) 定义

For Release configurations, this function returns the number of milliseconds since the device booted, excluding any time that the system was suspended. GetTickCount starts at 0 on boot and then counts up from there.

在Release版本中,该函数从0开始计时,返回自设备启动后的毫秒数(不含系统暂停时间)。

For Debug configurations, 180 seconds is subtracted from the the number of milliseconds since the device booted. This allows code that uses GetTickCount to be easily tested for correct overflow handling.

在Debug版本中,设备启动后便从计时器中减去180秒。这样方便测试使用该函数的代码的正确溢出处理。

Return Values

The number of milliseconds indicates success.

返回值:如正确,返回毫秒数。

Header: windows.h.
Link Library: Coredll.lib.

2) 应用

用来计算某个操作所使用的时间:  

  Start: = GetTickCount;  
   ...
// 执行耗时的操作  
   Stop: = GetTickCount;  
   TimeUsed:
= (Stop - Start) / 1000 ;     // 使用了xxx秒

用来定时 


void main()
{
  DWORD dwLast;
  DWORD dwCurrent;
  DWORD dwInterval
= 1000;
  dwLast
= GetTickCount();
 
int i = 0;
 
while(true)
    
{
        dwCurrent
= GetTickCount();
       
if( dwCurrent - dwLast < dwInterval )
       
continue;
        
//your code to be executed when interval is elapsed
        printf("dwLast,dwCurrent,diff:%d,%d,%d ",dwLast,dwCurrent,dwCurrent-dwLast);
        
//your code to determine when to break
        if( i > 10 ) break;
        i
++;
        dwLast
= dwCurrent;
        printf(
"Time is up!");
       
break;
     }

   getchar();  
  
return;
}

对于一般的实时控制,使用GetTickCount()函数就可以满足精度要求,但要进一步提高计时精度,就要采用QueryPerformanceFrequency()函数和QueryPerformanceCounter()函数。这两个函数是VC提供的仅供Windows   9X使用的高精度时间函数,并要求计算机从硬件上支持高精度计时器。

以上引用:

http://www.wesoho.com/article.asp?id=2072

http://bitboy.blog.edu.cn/user1/19986/archives/2005/1001846.shtml

 

#include "stdafx.h"
#include<cstdio>
#include<cstdlib>
#include<windows.h>
void main() 
{
  DWORD dwLast;
  DWORD dwCurrent;
  DWORD dwInterval = 1000;
  dwLast = GetTickCount();
  int i = 0;
  
  while(true)
     {
        dwCurrent = GetTickCount();
        if( dwCurrent - dwLast < dwInterval )
        continue;
						
        printf("dwLast,dwCurrent,diff:%d,%d,%d \n",dwLast,dwCurrent,dwCurrent-dwLast);


		for(int i=0;i<10000000;i++);

		dwCurrent = GetTickCount();
		printf("dwCurrent=%d\n",dwCurrent);
		
		break;
     }
   getchar();   
   return;
} 


 

DWORD   dwCurrent = GetTickCount(); 获取系统当前时间   ms为单位的计算

C++ GetTickCount()和Sleep()       

 

C++GetTickCount()和Sleep()

GetTickCount返回(retrieve)从操作系统启动到现在所经过(elapsed)的毫秒数,它的返回值是DWORDGetTickCount函数的原型为

DWORD GetTickCount(void);

它在winbase.h头文件中定义为

WINBASEAPI

DWORD

WINAPI

GetTickCount(

VOID

);

winbase.h已被包含进windows.h头文件中,所以要使用GetTickCount只需包含windows.h就可以了。

用一个DWORD类型的值存放一个时间数值,那么经历足够长的时间,这个数值一定会溢出绕回到零(wrap around to zero),我们可以计算这个时间。先算一下一天有多少毫秒mmsEachDay = 24*60*60*1000=86,400,000ms。而一个DWORD最大能表示多达数值呢?DWORD在头文件windef.h中定义如下
typedefunsignedlongDWORD;
可以看出DWORD是一个占四个字节即两个字的空间无符号整数,它能表示的最大值为232=4,294,967,695,而4,294,967,695/86,400,000 = 49.71,所以若系统持续运行(run continuously)49.71天,这个数会溢出绕回到零。
可以使用GetTickCount()来大概确定一段代码执行了多少时间,例程如下:

#i nclude <windows.h>

#i nclude <stdio.h>

//……..

DWORD dwStart;

DWORD dwEnd;

dwStart = GetTickCount();

printf( "dwStart:%d\n", dwStart );

//YOUR CODE TO BE TIMED;

dwEnd = GetTickCount();

printf( "dwEnd:%d\n", dwEnd );

printf( "elapsed:%d\n", dwEnddwStart )

也可以用GetTickCount函数来进行定时,比如若要定时1s,可以使用如下代码:

#i nclude <windows.h>

#i nclude <stdio.h>

void main()

{

DWORD dwLast;

DWORD dwCurrent;

DWORD dwInterval = 1000;

dwLast = GetTickCount();

int i = 0;

while(true)

{

dwCurrent = GetTickCount();

if( dwCurrent - dwLast < dwInterval )

continue;

//your code to be executed when interval is elapsed

printf("dwLast,dwCurrent,diff:%d,%d,%d\n",dwLast,dwCurrent,dwCurrent-dwLast);

//your code to determine when to break

if( i > 10 ) break;

i++;

dwLast = dwCurrent;

}

getchar();

return;

}

GetTickCount的分辨率是1ms。在精度要求不高的情况下也可以使用Sleep()函数定时,它的最小分辨率大概是55ms,Sleep的函数原型为
voidSleep( DWORD dwMilliseconds );
Sleep在winbase.h中的定义如下

WINBASEAPI

VOID

WINAPI

Sleep(

IN DWORD dwMilliseconds

);

其中VOID是基本数据类型void的别名,可以参见winnt.h中的定义

#ifndef VOID

#define VOID void

typedef char CHAR;

typedef short SHORT;

typedef long LONG;

#endif

如果要使用更高分辨率的话请使用多媒体定时器(multimedia timer)或高分辨率定时器(high-resolution timer)。
GetTickCount在.NET Framework中有一个对应项(counterpart)即System.Environment.TickCount属性。TickCount属性在Environment类中定义如下
publicstaticintTickCount {get;}
System.Environment.TickCount属性的值从系统计时器派生,并以 32 位有符号整数的形式存储。因此,如果系统连续运行 24.9 天,则运行时间将四舍五入到零位。TickCount 属性通过将其值重置为零来处理溢出状况。TickCount 返回的最小值为 0。MSDN上说TickCount 属性的分辨率不能小于 500 毫秒。我在程序中测试可以看出TickCount 的分辨率大概为15~16ms。测试代码如下[C#]

using System;

namespace ConsoleApplication2

{

/// <summary>

/// Class1 的摘要说明。

/// </summary>

class Class1

{

/// <summary>

/// 应用程序的主入口点。

/// </summary>

[STAThread]

static void Main(string[] args)

{

// TODO: 在此处添加代码以启动应用程序

for(int i=0;i<100;i++)

{

Console.WriteLine(i.ToString());

Console.WriteLine("start TickCount:" + System.Environment.TickCount.ToString());

System.Threading.Thread.Sleep(1);

Console.WriteLine("1ms elapsed TickCount:" + System.Environment.TickCount.ToString());

}

//pause for checking

System.Console.ReadLine();

}

}

}


 

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于您的问题,我可以告诉您如下方法: 首先,您需要导入OpenCV库。 然后,您需要编写代码来读取多张图片并执行蛮力匹配算法和FLANN匹配算法,并使用cv.getTickCount()函数来计算时间。 以下是一个简单的示例代码,用于演示如何计算多张照片蛮力匹配算法和FLANN匹配算法之间的时间效率: ```python import cv2 as cv # 读取图片 img1 = cv.imread('img1.jpg', cv.IMREAD_GRAYSCALE) img2 = cv.imread('img2.jpg', cv.IMREAD_GRAYSCALE) # 初始化ORB检测器 orb = cv.ORB_create() # 使用ORB检测器检测关键点和描述符 kp1, des1 = orb.detectAndCompute(img1, None) kp2, des2 = orb.detectAndCompute(img2, None) # 初始化蛮力匹配器 bf = cv.BFMatcher(cv.NORM_HAMMING, crossCheck=True) # 计算蛮力匹配器的时间 t1 = cv.getTickCount() matches = bf.match(des1, des2) t2 = cv.getTickCount() time_bf = (t2 - t1) / cv.getTickFrequency() # 初始化FLANN匹配器 index_params = dict(algorithm=0, trees=5) search_params = dict(checks=50) flann = cv.FlannBasedMatcher(index_params, search_params) # 计算FLANN匹配器的时间 t1 = cv.getTickCount() matches = flann.knnMatch(des1, des2, k=2) t2 = cv.getTickCount() time_flann = (t2 - t1) / cv.getTickFrequency() print("蛮力匹配器的时间:", time_bf) print("FLANN匹配器的时间:", time_flann) ``` 在该示例代码中,我们使用ORB检测器检测关键点和描述符,并使用cv.getTickCount()函数来计算蛮力匹配器和FLANN匹配器的时间。 请注意,该示例代码仅用于演示如何计算多张照片蛮力匹配算法和FLANN匹配算法之间的时间效率。您需要根据实际需求进行修改和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值