# Compare programs or subroutine

**URL:** <https://fortran-lang.discourse.group/t/compare-programs-or-subroutine/7882>\
**Category:** Help\
**Created:** [April 20, 2024, 7:18pm UTC](https://fortran-lang.discourse.group/t/compare-programs-or-subroutine/7882 "2024-04-20T19:18:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ezz](https://avatars.discourse-cdn.com/v4/letter/e/c67d28/32.png) [@ezz](https://fortran-lang.discourse.group/u/ezz)\
**Post date:** [April 20, 2024, 7:18pm UTC](https://fortran-lang.discourse.group/t/compare-programs-or-subroutine/7882/1 "2024-04-20T19:18:45Z")

</div>

Hello!  
In my library, Fig ([GitHub - AnonMiraj/fig: FIG (Fortran Intuitive Graphics)](https://github.com/AnonMiraj/fig)),  
I often need to compare different, similar approaches for their efficiency in terms of time and possibly space.

I was thinking of using a bash script to compare the time between two programs,

but I wonder if there is a good way to do it directly in Fortran.

---

<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:** [April 20, 2024, 7:25pm UTC](https://fortran-lang.discourse.group/t/compare-programs-or-subroutine/7882/2 "2024-04-20T19:25:43Z")

</div>

It depends if you are interested in comparing elapsed times or cpu times. Look up the intrinsic procedures `cpu_time()` and `system_clock()`.

---

<div class="post-metadata">

**Author:** ![hkvzjal](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/hkvzjal/32/3055_2.png) [@hkvzjal](https://fortran-lang.discourse.group/u/hkvzjal)\
**Post date:** [April 20, 2024, 7:32pm UTC](https://fortran-lang.discourse.group/t/compare-programs-or-subroutine/7882/3 "2024-04-20T19:32:13Z")

</div>

> [@How to show the true wall time instead of cpu\_time?](https://fortran-lang.discourse.group/t/how-to-show-the-true-wall-time-instead-of-cpu-time/2189/7):
>
> A few years ago, I compared time measurement subroutines in Fortran. I recommend date\_and\_time or omp\_get\_wtime (OpenMP’s time measurement function) as time measurement subroutines. For system\_clock, the count rate is different depending on the compiler. Especially for PGI (currently nvfortran), the upper limit of measurement time is about 36 minutes when integer(int32) is used for the count. Also, system\_clock and cpu\_time cannot measure the execution time of a parallelized program. The tabl…

You might find this thread very insightful for a comparison of several methods to measure elapsed time.

---

<div class="post-metadata">

**Author:** ![Ali](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ali/32/751_2.png) [@Ali](https://fortran-lang.discourse.group/u/Ali)\
**Post date:** [April 20, 2024, 11:00pm UTC](https://fortran-lang.discourse.group/t/compare-programs-or-subroutine/7882/4 "2024-04-20T23:00:45Z")

</div>

There are several fpm packages available. You might want to try ForTime: [GitHub - gha3mi/fortime: ForTime - A Fortran library for measuring elapsed time, DATE\_AND\_TIME time, CPU time, OMP time and MPI time.](https://github.com/gha3mi/fortime)

Here’s how to use it simply:

```auto
use fortime
type(timer) :: t

call t%timer_start()
! Your code or section to be timed
call t%timer_stop(nloops, message, print=.true.) ! nloops, message and print are optional

```

For benchmarking, you might want to try ForBenchmark: [GitHub - gha3mi/forbenchmark: ForBenchmark - A Fortran library for benchmarking (with support for coarrays).](https://github.com/gha3mi/forbenchmark)

---

<div class="post-metadata">

**Author:** ![ezz](https://avatars.discourse-cdn.com/v4/letter/e/c67d28/32.png) [@ezz](https://fortran-lang.discourse.group/u/ezz)\
**Post date:** [April 21, 2024, 5:26pm UTC](https://fortran-lang.discourse.group/t/compare-programs-or-subroutine/7882/5 "2024-04-21T17:26:36Z")

</div>

thank you  
this is exactly what i am looking for

---

<div class="post-metadata">

**Author:** ![Hendrik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/hendrik/32/4664_2.png) [@Hendrik](https://fortran-lang.discourse.group/u/Hendrik)\
**Post date:** [April 21, 2024, 8:55pm UTC](https://fortran-lang.discourse.group/t/compare-programs-or-subroutine/7882/6 "2024-04-21T20:55:04Z")

</div>

I wrote many moons ago a simple module for timings.

[https://bazaar.launchpad.net/~hwkrus/dolfyn-cfd/trunk/view/head:/src/watches.f90](https://bazaar.launchpad.net/~hwkrus/dolfyn-cfd/trunk/view/head:/src/watches.f90)

At the end of the run it displays how much time was spent where (either the whole subroutine, or a loop or a call). An example:

```auto
 Elapsed time: 35861.20    
  W L RA : cpu %
  8 2 FF ** : CalculateUVW 5.558E+03 15.50
 15 3 FF *** : MKL gmres 5.489E+03 15.30
 20 2 FF ** : CalculateScalar 3.680E+03 10.26
 16 3 FF *** : GaussPressCor 2.877E+03 8.02
...
  1 1 FF * : Master 1.406E+03 3.92
...
  2 2 FF ** : ReadGeometry 1.736E+00 0.00
                                            --------- -----
                                            3.586E+04 99.99

```

The asterisk depicts the depth or level (L) of the timer.

---

<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:** [April 22, 2024, 1:06am UTC](https://fortran-lang.discourse.group/t/compare-programs-or-subroutine/7882/7 "2024-04-22T01:06:02Z")

</div>

@hkvzjal  
I would recommend you look to other threads, as of the timing routines mentioned in this thread, system\_clock is probably the best routine available.  
You need to test the different integer kind arguments, as this can vary the accuracy and range.

Using 8-byte integers in Gfortran gives a comparitively accurate result. I have posted elsewhere comparing system\_clock to date\_and\_time or omp\_get\_wtime, which both exhibit poor relative precision on the compilers I use. A number of Windows based timers are limited to 64 value updates per second, which often gives a poor result.

The quoted performance for PGI of being limited to 36 minutes [of elapsed time?] looks very suspicious, which is another red flag for this thread. I have not used the PGI compiler.

Timer performance varies greatly between operating systems.
