计算函数的运行时间

在写代码中,有时候我们需要评估某段代码或者函数的执行时间;方法就是在该段代码或者函数前面,记录一个时间T1,在代码段或函数后面记录时间T2,那其运行时间就是T2-T1;

就是简单的减法!!!

那具体的实现方法呢?我这里有三个,给大家参考:

一,clock();

clock()是C/C++中的计时函数,而与其相关的数据类型是clock_t;头文件:time.h;

typedef long clock_t;可见clock_t为长整型;

在time.h文件中,还定义了一个 常量 CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元,其定义如下:
#define CLOCKS_PER_SEC ((clock_t)1000)
例子:
#include <stdio.h>
#include < stdlib.h>
#include <time.h>
int main(void)
{
long i = 10000000L;
clock_t start, finish;
double duration;
/* 测量一个事件持续的时间*/
printf( "Time to do %ld empty loops is ", i) ;
start = clock();
while( i-- );
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%f seconds\n", duration );
system("pause");
}
 
二,另一种形式就是timeval结构体,定义如下:
struct timeval
{
time_t tv_sec; /* Seconds. */
suseconds_t tv_usec; /* Microseconds. */
};
例子:
#include <stdio.h>
#include <sys/time.h>
#include <time.h>

int main(int argc, char * argv[])
{
struct timeval tv; //定义
while(1){
gettimeofday(&tv, NULL); //获取时间
printf("time %u:%u\n", tv.tv_sec, tv.tv_usec);
sleep(2);
}
return 0;
}
 
两种方法的区别:
1,若是粗略的计算,都可以使用;
2,区别在于定义上:clock的最小精度为毫秒(ms);使用的节拍来定义;
timeval精确到微秒(us),获取的是系统时间,而且还有秒;

三、也可以封装一个计时类

class CMyCountTime
{
   private:
        clock_t m_time;;
   public:
       CMyCountTime(){m_time=clock();}
       ~CMyCountTime()
       {
         m_time=clock()-m_time;
         cout<<m_time/CTK_CLK<<endl;
       }

 }

{
  CMyCountTime mytime;
  运行被测试程序代码
 }
自动析构得出运行时间

以下是一个简单的demo,演示如何在Photo Sphere Viewer 5.1中按住鼠标移动Marker位置: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Photo Sphere Viewer 5.1 Marker Demo</title> <style> #viewer { width: 100%; height: 500px; } </style> </head> <body> <div id="viewer"></div> <script src="https://cdn.jsdelivr.net/npm/three/build/three.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/photo-sphere-viewer/dist/photo-sphere-viewer.min.js"></script> <script> // 创建Photo Sphere Viewer对象 var viewer = new PhotoSphereViewer({ container: 'viewer', panorama: 'https://photo-sphere-viewer.js.org/assets/Bryce-Canyon-National-Park-Mark-Doliner.jpg', navbar: true, markers: [ { id: 'marker1', longitude: 0.5, latitude: 0.2, image: 'https://photo-sphere-viewer.js.org/assets/pin-red.png', width: 32, height: 32, anchor: 'bottom center', }, ], }); // 获取标记对象 var marker = viewer.getMarker('marker1'); // 在mousedown事件上更新标记位置 marker.on('mousedown', function(event) { // 阻止默认行为和事件传播 event.preventDefault(); event.stopPropagation(); // 获取场景坐标系中的鼠标位置 var mousePosition = viewer.mouseToCoords(event.clientX, event.clientY); // 更新标记位置 marker.setPosition(mousePosition); }); // 在mouseup事件上保存标记位置 marker.on('mouseup', function(event) { // 保存标记位置 marker.save(); }); </script> </body> </html> ``` 在这个demo中,我们在Photo Sphere Viewer中添加了一个标记,并使用上面提到的代码来实现按住鼠标移动Marker位置的功能。您可以在https://codepen.io/pen/GRvKyNL 上查看该demo的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值