当有小时、分钟、秒和毫秒时计算持续时间

发布于 2024-11-15 03:20:27 字数 792 浏览 5 评论 0原文

我正在用 Fortran 编写一个程序,我需要一种将程序的持续时间计算到毫秒的方法。我一直在使用函数“date_and_time”,它给我留下了一个包含系统时间(以小时、分钟、秒和毫秒为单位)的数组。

我相信我可以在程序开始时调用这个函数来存储当前时间,然后在程序结束时再次调用该函数来存储最新时间。但在那之后,我将如何计算持续时间?我尝试只减去这些值,但是当一秒过去时毫秒会重置,就像一分钟过去时秒重置一样。解决这个问题的最佳方法是什么?

这是程序:

       PROGRAM TEST_TIME_AND_DATE
          INTEGER I
          REAL J
          INTEGER TIME_A(8), TIME_B(8)
          CALL DATE_AND_TIME(VALUES=TIME_A)
          PRINT '(8I5))', TIME_A
          DO I = 0, 400000000
            J = I * I - J
          END DO
          CALL DATE_AND_TIME(VALUES=TIME_B)
          print '(8I5))', TIME_B
      END PROGRAM TEST_TIME_AND_DATE

这是结果:

2011    6   11 -300    9   14   49  304
2011    6   11 -300    9   14   50  688

我不知道在这里做什么,谢谢。

I am writing a program in Fortran and I need a way of calculating the duration of the program down to milliseconds. I have been using the function "date_and_time", which leaves me with an array containing the system's time in hours, minutes, seconds, and milliseconds.

I believe that I can call this function at the start of my program to store the current time, then call the function again at the end of my program to store the latest time. But after that, how would I computer the duration? I tried just subtracting the values, but the milliseconds reset when one second passes, just like the seconds reset when one minute passes. How would be best way to approach this be?

Here is the program:

       PROGRAM TEST_TIME_AND_DATE
          INTEGER I
          REAL J
          INTEGER TIME_A(8), TIME_B(8)
          CALL DATE_AND_TIME(VALUES=TIME_A)
          PRINT '(8I5))', TIME_A
          DO I = 0, 400000000
            J = I * I - J
          END DO
          CALL DATE_AND_TIME(VALUES=TIME_B)
          print '(8I5))', TIME_B
      END PROGRAM TEST_TIME_AND_DATE

And here is the result:

2011    6   11 -300    9   14   49  304
2011    6   11 -300    9   14   50  688

I'm not sure what to do here, thanks.

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

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

发布评论

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

评论(3

铁轨上的流浪者 2024-11-22 03:20:27

如果您想要经过的时钟时间,使用内部过程 system_clock 会更简单,因为它提供单个时间值输出。 (还有其他参数来提供有关过程的信息,这就是为什么它是过程而不是函数的原因。)例如,请参见 http://gcc.gnu.org/onlinedocs/gfortran/SYSTEM_005fCLOCK.html。如果您想对 CPU 使用情况进行计时,请使用 cpu_time。对于任一情况,在程序开始和结束时进行两次调用,然后进行简单的区别。您可以使用 COUNT_RATE 参数将时间的整数计数转换为秒。

If you want elapsed clock time, it would be simpler to use the intrinsic procedure system_clock since it provides a single time-value output. (There are additional arguments to provide information about the procedure, which is why it is a procedure instead of a function.) See, for example, http://gcc.gnu.org/onlinedocs/gfortran/SYSTEM_005fCLOCK.html. If you want to time the CPU usage, then use cpu_time. For either, two calls, at the start and end of the program, then a simple difference. You can use the COUNT_RATE argument to convert to integer count of time into seconds.

无名指的心愿 2024-11-22 03:20:27

您可以减去数字,然后将所有内容转换为毫秒,然后将毫秒、秒以毫秒为单位、分钟以毫秒为单位、小时以毫秒为单位求和,...

在您的情况下,这将是

0 + 0 + 0 + 0 + 0 + 1*1000 + 384 = 1384 [ms]

这种方法也适用于溢出,因为正数在如果所有负数都转换为相同的基数,则更左边的列的权重会超过负数。例如,0:58.0001:02.200 的产量

1 * 60000 + (-56) * 1000 + 200 = 4200

请注意,这最多可以工作几天,但不能工作几个月,因为它们不具有共同的长度。

You can subtract the numbers, then convert everything into milliseconds and sum up the ms, sec in ms, min in ms, hrs in ms, ...

In your case this would be

0 + 0 + 0 + 0 + 0 + 1*1000 + 384 = 1384 [ms]

This approach works fine also with overflows since a positive number in a left-more column outweights negative numbers if they are all converted to the same basis. E.g. 0:58.000 to 1:02.200 yields

1 * 60000 + (-56) * 1000 + 200 = 4200

Please note that this does work up to days but not with months since they do not share a common length.

满身野味 2024-11-22 03:20:27

您可以计算与某个开始时间(UNIX 的 1970 年 1 月 1 日)的偏移量(以秒或毫秒为单位)。这些数字的差异就是您所用的时间。

(2011 - 1970) * (number of seconds in a year) +
(month of the year - 1) * (number of seconds in a month) +
(day of the month - 1) * (number of seconds in a day) + 
( ... )

You could calculate the offset from some starting time (Jan 1, 1970 for UNIX) in seconds or milliseconds. The difference in those numbers is your elapsed time.

(2011 - 1970) * (number of seconds in a year) +
(month of the year - 1) * (number of seconds in a month) +
(day of the month - 1) * (number of seconds in a day) + 
( ... )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文