Compare programs or subroutine

Hello!
In my library, Fig (GitHub - AnonMiraj/fig: FIG (Fortran Intuitive Graphics)),
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.

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

1 Like

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

2 Likes

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.

Here’s how to use it simply:

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).

1 Like

thank you
this is exactly what i am looking for

1 Like

I wrote many moons ago a simple module for timings.

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:

 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.

1 Like

@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.

1 Like