开发者

Calculating duration when you have hours, minutes, seconds, and milliseconds

开发者 https://www.devze.com 2023-03-12 19:06 出处:网络
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 conta

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.


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.


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.


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) + 
( ... )
0

精彩评论

暂无评论...
验证码 换一张
取 消