Why Cpu_time function gives incorrect time

when i use CALL cpu_time(T2) it gives an incorrect time
i used Vb and gfortran to compile the code

In my experiments with gfortran cpu_time gives the total time across all cores. system_clock can be used for wall time.

1 Like

Agreed, cpu_time is not that useful for measuring wall-clock time compared to the following format:

integer(int64) :: start_count, rate, stop_count
call system_clock(count=start_count, count_rate=rate)
!! code to be benchmarked
call system_clock(count=stop_count)
write(*,*) 'benchmark time: ',real(max(stop_count - start_count, 1_int64))/real(rate),' seconds'

The cpu_time subroutine really does what the name says, it measures the CPU time, similar to what you would see e.g. when executing top or ps. Not only it sums the CPU time from all threads, it will also stop acquiring any new time when the thread is not active and some other process uses the CPU instead. In this regard the values reported by cpu_time may actually be more useful than those from system_clock when testing performance of some pieces of code when other processes interfere with the measurement.

There are many possible timings that are useful. In a time-sharing environment, the cpu time is typically just the time that your process is executing, not the time when your job is swapped out or the time waiting for some i/o operation to complete. So when you run the same job multiple times, you get (almost) the same values whether the machine is running your job alone or in a mix with many other jobs. In a parallel environment with multiple threads, it makes sense then for the cpu time to include the total. But what about a multiple process or multiple node environment? It could return just the cpu time on a single process, or the sum over all processes on a single node, or even the sum over all the nodes. All of those numbers might be useful information to either the programmer testing/tuning an algorithm or to a user executing the application code and paying for the resources that are consumed by his job.

CPU_time and System_Clock record different measures of process time. Each is appropriate for different measures. It is also useful to understand the inaccuracy of these implementations.

You may find the previous discussion in the link below of use.
[Proper Usage of `system_clock`]