当有小时、分钟、秒和毫秒时计算持续时间
我正在用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想要经过的时钟时间,使用内部过程 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.
您可以减去数字,然后将所有内容转换为毫秒,然后将毫秒、秒以毫秒为单位、分钟以毫秒为单位、小时以毫秒为单位求和,...
在您的情况下,这将是
这种方法也适用于溢出,因为正数在如果所有负数都转换为相同的基数,则更左边的列的权重会超过负数。例如,
0:58.000
到1:02.200
的产量请注意,这最多可以工作几天,但不能工作几个月,因为它们不具有共同的长度。
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
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
to1:02.200
yieldsPlease note that this does work up to days but not with months since they do not share a common length.
您可以计算与某个开始时间(UNIX 的 1970 年 1 月 1 日)的偏移量(以秒或毫秒为单位)。这些数字的差异就是您所用的时间。
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.