Timing
February 2020
Back to index.html.
Timing
In this chapter, we are looking at methods to measure timing.
CPU time
According to Wikipedia, “CPU time (or process time) is the amount of time for which a central processing unit (CPU) was used for processing instructions of a computer program or operating system”. Another timing is elapsed time, also called wall-clock time, which is the actual time from the start of the program to the end. Merely speaking, CPU time is the net time for which CPU is working, and it is not the same as elapsed time, including waits of CPU by requests of operating systems.
There is an intrinsic subroutine to get the current CPU time.
Name | Description | Usage |
---|---|---|
cpu_time() |
Get the current CPU time | call cpu_time(x) |
It returns CPU time to a real variable. If you want to measure CPU time for a job, you can call the subroutine twice, at the beginning and end of the job. The difference between the two timings is CPU time for the job.
program timing
implicit none
real :: begin_time,end_time
call cpu_time(begin_time)
! do something heavy
...
call cpu_time(end_time)
print *,"CPU time = ",end_time - begin_time
end program timing
In heavy computations that entirely use CPU, CPU time is nearly equivalent to the elapsed time. However, when the CPU tends to be an idol, such as file reading, the CPU time is shorter than the elapsed time. Also, is the job uses multiple CPU (or CPU cores), CPU time is the sum of CPU times for all CPU.
System clock
An intrinsic subroutine, system_clock
, can return a clock count generated by a system clock equipped in a computer. A computer manages the current time as the number of seconds or milliseconds from a fixed date and time like 0 AM on January 1, 1970.
Name | Description | Usage |
---|---|---|
system_clock() |
Get the current clock count | call system_clock(c) |
This function has two more optional arguments.
call system_clock(c,count_rate=cr)
: integercr
represents the precision of clock e.g., the number of clock counts per second or so.call system_clock(c,count_max=cm)
: integercm
represents the maximum ofc
. Ifc
exceedscm
, the clock count will be reset to 0.
To measure the timing of a task, use system_clock
as cpu_time
, and adjust the difference by cr
.
program timing
implicit none
integer :: begin_clock,end_clock,cr
call system_clock(begin_clock)
! do something heavy
...
call system_clock(end_clock,count_rate=cr)
print *,"CPU time = ",(end_clock - begin_clock)/real(cr)
end program timing
There are several cautions to use this method.
- The clock counts are integer, so you have to convert them to real numbers with
real()
ordble()
. - Although the timing in this method is nearly equivalent to elapsed time, it may be inaccurate for a very short time, less than a few seconds. The system clock is not correctly working; especially, the system is busy.
- Also, there is a risk of overflow in the system clock. To avoid it, use a 64bit integer variable for clock counters (see below).
program timing
use,intrinsic :: iso_fortran_enc
implicit none
integer(int64) :: begin_clock,end_clock,cr
...
Back to index.html.