Back to index.html.
In this chapter, we are looking at methods to measure timing.
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.
||Get the current CPU time||
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.
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.
||Get the current clock count||
This function has two more optional arguments.
call system_clock(c,count_rate=cr): integer
crrepresents the precision of clock e.g., the number of clock counts per second or so.
call system_clock(c,count_max=cm): integer
cmrepresents the maximum of
cm, the clock count will be reset to 0.
To measure the timing of a task, use
cpu_time, and adjust the difference by
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
- 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.