# Why Cpu\_time function gives incorrect time

**URL:** <https://fortran-lang.discourse.group/t/why-cpu-time-function-gives-incorrect-time/6754>\
**Category:** Help\
**Created:** [November 8, 2023, 10:46pm UTC](https://fortran-lang.discourse.group/t/why-cpu-time-function-gives-incorrect-time/6754 "2023-11-08T22:46:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mahmoud96](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@Mahmoud96](https://fortran-lang.discourse.group/u/Mahmoud96)\
**Post date:** [November 8, 2023, 10:46pm UTC](https://fortran-lang.discourse.group/t/why-cpu-time-function-gives-incorrect-time/6754/1 "2023-11-08T22:46:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rbitr](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rbitr](https://fortran-lang.discourse.group/u/rbitr)\
**Post date:** [November 8, 2023, 11:34pm UTC](https://fortran-lang.discourse.group/t/why-cpu-time-function-gives-incorrect-time/6754/2 "2023-11-08T23:34:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tyranids](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@tyranids](https://fortran-lang.discourse.group/u/tyranids)\
**Post date:** [November 9, 2023, 1:30am UTC](https://fortran-lang.discourse.group/t/why-cpu-time-function-gives-incorrect-time/6754/3 "2023-11-09T01:30:16Z")

</div>

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

```auto
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'

```

---

<div class="post-metadata">

**Author:** ![VladimirF](https://avatars.discourse-cdn.com/v4/letter/v/e9a140/32.png) [@VladimirF](https://fortran-lang.discourse.group/u/VladimirF)\
**Post date:** [November 9, 2023, 2:41pm UTC](https://fortran-lang.discourse.group/t/why-cpu-time-function-gives-incorrect-time/6754/4 "2023-11-09T14:41:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [November 9, 2023, 5:24pm UTC](https://fortran-lang.discourse.group/t/why-cpu-time-function-gives-incorrect-time/6754/5 "2023-11-09T17:24:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JohnCampbell](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@JohnCampbell](https://fortran-lang.discourse.group/u/JohnCampbell)\
**Post date:** [November 10, 2023, 5:56am UTC](https://fortran-lang.discourse.group/t/why-cpu-time-function-gives-incorrect-time/6754/6 "2023-11-10T05:56:25Z")

</div>

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`](https://fortran-lang.discourse.group/t/proper-usage-of-system-clock/3245)]
