如何减去两个gettimeofday实例? [英] How to subtract two gettimeofday instances?

查看:240
本文介绍了如何减去两个gettimeofday实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想减去两个gettimeofday实例,并以毫秒为单位提供答案。

I want to subtract two gettimeofday instances, and present the answer in milliseconds.

这个想法是:

  static struct timeval tv;
  gettimeofday(&tv, NULL);

  static struct timeval tv2;
  gettimeofday(&tv2, NULL);

  static struct timeval tv3=tv2-tv;

然后将'tv3'转换为毫秒分辨率。

and then convert 'tv3' into milliseconds resolution.

推荐答案

您可以使用glibc提供的timersub()函数,然后将结果转换为毫秒(尽管如此,请注意溢出)。

You can use the timersub() function provided by glibc, then convert the result to milliseconds (watch out for overflows when doing this, though!).

这篇关于如何减去两个gettimeofday实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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