如何计算C ++中的时间差 [英] How to calculate a time difference in C++

查看:235
本文介绍了如何计算C ++中的时间差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中计算时间差的最好方法是什么?我计时程序的执行速度,所以我感兴趣的毫秒。更好,seconds.milliseconds ..

What's the best way to calculate a time difference in C++? I'm timing the execution speed of a program, so I'm interested in milliseconds. Better yet, seconds.milliseconds..

接受的答案工作,但需要包括ctime或time.h在注释中注释。

The accepted answer works, but needs to include ctime or time.h as noted in the comments.

推荐答案

请参阅 std :: clock() 函数。

See std::clock() function.

const clock_t begin_time = clock();
// do something
std::cout << float( clock () - begin_time ) /  CLOCKS_PER_SEC;

如果你想计算self的执行时间(不是为用户),最好是时钟滴答(而不是秒)。

If you want calculate execution time for self ( not for user ), it is better to do this in clock ticks ( not seconds ).

EDIT:

负责的头文件 - < ctime> < time.h>

这篇关于如何计算C ++中的时间差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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