C++计时函数

C++ error LNK2001:无法解析的外部符号_imp_timeGetTime的解决办法
在文件头部添加 
#pragma comment(lib, "winmm.lib")
#include <windows.h>
https://blog.csdn.net/qq_34504481/article/details/80924302

void test1()
{
    time_t start,stop;
    start = time(NULL);
    foo();//dosomething
    stop = time(NULL);
    printf("Use Time:%ld\n",(stop-start));
}

void test3()
{
    DWORD t1,t2;
    t1 = timeGetTime();
    foo();//dosomething
    t2 = timeGetTime();
    printf("Use Time:%f\n",(t2-t1)*1.0/1000);
}
https://www.cnblogs.com/dwdxdy/p/3214905.html

Linux C语言之计算程序段运行时间

struct timeval start;
struct timeval end;
float timer;

gettimeofday(&start,NULL);
func();
gettimeofday(&end,NULL);
timer = end.tv_sec - start.tv_sec + (float)(end.tv_usec - start.tv_usec)/1000000;

printf("timer = %fs\n",timer);
https://blog.csdn.net/donghanhang/article/details/50826112


auto t_start2 = std::chrono::high_resolution_clock::now();

程序

auto t_end2 = std::chrono::high_resolution_clock::now();
float ms2 = std::chrono::duration<float, std::milli>(t_end2 - t_start2).count();
VE_LOGI("FrcDataPrepare time : %f \n", ms2);

===========================================================================================

方法一(用这个方法):
#include <opencv2/opencv.hpp>
using namespace cv;

double t = (double)getTickCount();

fun();

t = ((double)getTickCount() - t) / getTickFrequency();
VE_LOGI("VeFrcProcess init_time=%f s.\n", t);

方法二:
#include <sys/time.h>

const int CLOCKS_PER_SEC_FIX = 1;
const int MILLISEC_PER_SEC = 1000;
const int MICROSEC_PER_SEC = 1000000;
struct timeval g_initStart, g_initEnd, g_processEnd;

gettimeofday(&g_initStart, nullptr);

fun();

gettimeofday(&g_initEnd, nullptr);
float initTime = (g_initEnd.tv_sec - g_initStart.tv_sec) * MICROSEC_PER_SEC + (g_initEnd.tv_usec - g_initStart.tv_usec);
printf("init use time = %f ms.\n", initTime * MILLISEC_PER_SEC / (CLOCKS_PER_SEC * CLOCKS_PER_SEC_FIX));
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WX Chen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值