测量时间差

发布于 2024-10-08 03:20:11 字数 110 浏览 6 评论 0原文

我想以毫秒为单位测量启动应用程序与另一个时间(例如 16:00 点)之间的时间间隔。最好的方法是什么?

我查看了“时钟”功能,但这不是我需要的。

操作系统:Win NT及以上

I want to measure in milliseconds how much time there is between when I start my application and another time, for example 16:00 o'clock. What is the best way to do this?

I looked around "clock" function but it's not what I need.

Operating system: Win NT and above

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你另情深 2024-10-15 03:20:11

查找 POSIX 系统的 gettimeofday ,以及 timeGetTime(适用于 Windows)。

编辑:OP似乎要求提供代码来将当前时间/日期与另一个时间/日期进行比较。以下代码片段演示了如何在 Windows 上获取当前日期和时间:

 #include ;
 #include ;

 无效主()
 {
     系统时间st;
     获取系统时间(&st);
     printf("年:%d\n月:%d\n日期:%d\n小时:%d\n分钟:%d\n秒:%d\n"
       ,st.w年,st.w月,st.w日,st.w小时,st.w分,st.w秒);
 }

下面是如何计算两个 SYSTEMTIME 对象之间的差异:

 #include ;
 #include ;

 // 返回时间差,单位为 100 us。
 _int64 Delta(const SYSTEMTIME st1, const SYSTEMTIME st2) {
  联合时间联合 {
      FILETIME 文件时间;
      ULARGE_INTEGER ul;
  };

  时间联盟 ft1;
  时间联盟 ft2;

  SystemTimeToFileTime(&st1, &ft1.fileTime);
  SystemTimeToFileTime(&st2, &ft2.fileTime);

  返回 ft2.ul.QuadPart - ft1.ul.QuadPart;
}

int main() {
  系统时间 t1 = {0},t2 = {0};
  t1.wDay = 10;
  t1.wMonth = 4;
  t1.wYear = 2009;

  t2.wDay = 12;
  t2.wMonth = 4;
  t2.wYear = 2009;

  _int64 i = Delta(t1, t2);
  std::cout << “时间是”<< i / 10000000 << " 相隔几秒\n";

  返回0;
}

这两个示例应该为您提供完成您需要的工具。

Look up gettimeofday for POSIX systems, and timeGetTime for Windows.

Edit: Seems the OP was asking for code to compare current time/date against another time/date. The following snippet demonstrates how to get current date and time on Windows:

 #include <Windows.h>
 #include <stdio.h>

 void main()
 {
     SYSTEMTIME st;
     GetSystemTime(&st);
     printf("Year:%d\nMonth:%d\nDate:%d\nHour:%d\nMin:%d\nSecond:%d\n"
       ,st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
 }

And here's how to compute the difference between two SYSTEMTIME objects:

 #include <windows.h>
 #include <iostream>

 // Return time difference in units of 100 us.
 _int64 Delta(const SYSTEMTIME st1, const SYSTEMTIME st2) {
  union timeunion {
      FILETIME fileTime;
      ULARGE_INTEGER ul;
  } ;

  timeunion ft1;
  timeunion ft2;

  SystemTimeToFileTime(&st1, &ft1.fileTime);
  SystemTimeToFileTime(&st2, &ft2.fileTime);

  return ft2.ul.QuadPart - ft1.ul.QuadPart;
}

int main() {
  SYSTEMTIME t1 = {0}, t2 = {0};
  t1.wDay = 10;
  t1.wMonth = 4;
  t1.wYear = 2009;

  t2.wDay = 12;
  t2.wMonth = 4;
  t2.wYear = 2009;

  _int64 i = Delta(t1, t2);
  std::cout << "times are " << i / 10000000 << " seconds apart\n";

  return 0;
}

Those two samples should give you the tools to do what you need.

篱下浅笙歌 2024-10-15 03:20:11

如果您使用的是 POSIX 系统,则可以使用 gettimeofday(3)

struct timeval start, end;
gettimeofday(&start, NULL);
...
gettimeofday(&end, NULL);

// Watch out for overflow!
int delta_milliseconds = 1000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec)/1000;

如果您使用的是 Windows,则可以使用 GetTickCount

DWORD start, end;
start = GetTickCount();
...
end = GetTickCount();
int delta_milliseconds = end - start;

但是要注意 GetTickCount 的分辨率只有大约 10-16 毫秒。如果您需要更高的精度,请使用 QueryPerformanceCounterQueryPerformanceFrequency 相反:

// Error-checking omitted for expository purposes
LARGE_INTEGER freq, start, end;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&start);
...
QueryPerformanceCounter(&end);
double delta_milliseconds = (double)(end.QuadPart - start.QuadPart) / freq.QuadPart * 1000.0;

If you're on a POSIX system, you can use gettimeofday(3):

struct timeval start, end;
gettimeofday(&start, NULL);
...
gettimeofday(&end, NULL);

// Watch out for overflow!
int delta_milliseconds = 1000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec)/1000;

If you're on Windows, you can use GetTickCount:

DWORD start, end;
start = GetTickCount();
...
end = GetTickCount();
int delta_milliseconds = end - start;

But but aware that GetTickCount only has a resolution of about 10-16 ms. If you need more precision, use QueryPerformanceCounter and QueryPerformanceFrequency instead:

// Error-checking omitted for expository purposes
LARGE_INTEGER freq, start, end;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&start);
...
QueryPerformanceCounter(&end);
double delta_milliseconds = (double)(end.QuadPart - start.QuadPart) / freq.QuadPart * 1000.0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文