Ubuntu C/C++ 获取系统时间

使用OpenCV处理图像的时,可以使用linux系统的头文件获取图像处理时间。

#include 

Code Example:

#include 
#include 

int main(int argc, char **argv) {
  struct timeval tv;
  gettimeofday(&tv,NULL);
  printf("second:%ld \n",tv.tv_sec); //秒
  printf("millisecond:%ld \n",tv.tv_sec*1000 + tv.tv_usec/1000); //毫秒
  printf("microsecond:%ld \n",tv.tv_sec*1000000 + tv.tv_usec); //微秒
  return 0;
}

Result:

second:1545039819 
millisecond:1545039819224 
microsecond:1545039819224055 

gettimeofday函数

Name: gettimeofday
Prototype: int gettimeofday (struct timeval *tp, struct timezone *tzp)
Description:
The gettimeofday function returns the current calendar time as the elapsed time since the epoch in the struct timeval structure indicated by tp. ( for a description of struct timeval). Information about the time zone is returned in the structure pointed at tzp. If the tzp argument is a null pointer, time zone information is ignored.

The return value is 0 on success and -1 on failure. The following errno error condition is defined for this function.

------------------------------------------------------20190603--------------------------------------------------------------------------------------------------

在ROS机器人中如何使用

  struct timeval start, end;
  double start_t, end_t, t_diff;

  gettimeofday(&start, NULL);
  for(unsigned int i = 0; i < 2000; ++i){
    pg.insert(point);
  }
  gettimeofday(&end, NULL);
  start_t = start.tv_sec + double(start.tv_usec) / 1e6;
  end_t = end.tv_sec + double(end.tv_usec) / 1e6;
  t_diff = end_t - start_t;
  printf("%%Insertion Time: %.9f \n", t_diff);

 

你可能感兴趣的:(C++基础)