您现在的位置是:首页 >要闻 > 2023-12-01 04:36:19 来源:

gettickcount函数(gettickcount)

导读 大家好,我是小夏,我来为大家解答以上问题。gettickcount函数,gettickcount很多人还不知道,现在让我们一起来看看吧!1、原因是 LZ 对...

大家好,我是小夏,我来为大家解答以上问题。gettickcount函数,gettickcount很多人还不知道,现在让我们一起来看看吧!

1、原因是 LZ 对机器太有爱心,测试个程序就给那么丁点“活儿”给它,太瞧不起它了~~

2、这点工作, 现在的 CPU 水平不要几个 ms 就能完成,只要 LZ 运行时机器不卡,下面的工作会在 16 个 ms 内完成,所以 begin 和 end 返回同样的值

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

4、 {

5、 for(int j=0; j<100; j++)

6、 {

7、 a[i][j] = i+j;

8、 }

9、 }

10、根据 MSDN 上面对于 GetTickCount 函数的描述:

11、 “…… in the range of 10 milliseconds to 16 milliseconds ……”

12、http://msdn.microsoft.com/en-us/library/ms724408%28VS.85%29.aspx

13、改函数测试时间的精度是 10~16 ms,所以,如果你两次调用 GetTickCount() 之间的工作如果在 16 ms 内完成,那么返回的时间差就有可能为 0

14、LZ 只需要稍作修改,给 cpu 加点‘活’就能看出时间消耗了,比如下面

15、以下代码 VS2010 编译通过,运行正常

16、#include <windows.h>

17、#include <iostream>

18、using namespace std;

19、int main()

20、{

21、 int a[200][200]; // 数组改大点,不要担心你的宝贝机器会受不了,如果只是区区赋值而已,它眼都不眨一气呵成。

22、 DWORD begin,end,time;

23、 begin = GetTickCount();

24、 for(int i =0; i<200; i++)

25、 {

26、 for(int j=0; j<200; j++)

27、 {

28、 a[i][j] = i+j;

29、 std::cout << a[i][j] << std:: endl; // 让它一个一个输出,这个可是比较可观的“活儿”

30、 }

31、 }

32、 end = GetTickCount();

33、cout<<"begin="<<begin<<endl;

34、 cout<<"end="<<end<<endl;

35、 time = end - begin;

36、 cout<<time;

37、 return 0;

38、}

39、运行结果:

40、…………… // 前面的省了

41、 389

42、 390

43、 391

44、 392

45、 393

46、 394

47、 395

48、 396

49、 397

50、 398

51、 begin=12981375

52、 end=12990625

53、 9250请按任意键继续. . .

54、我的老爷机,LZ 请无视结果

本文到此讲解完毕了,希望对大家有帮助。