CFD Online Discussion Forums

CFD Online Discussion Forums (
-   Main CFD Forum (
-   -   Elapsed time (

Pam April 19, 2005 12:53

Elapsed time
It should be a simple question for experts, but as a beginner I need to ask. How can I get the total elapsed rum time of my computational code in FORTRAN with g77 compiler. I used "mclock", but it is just good for cpu time, not the real elapsed time in seconds. Also, RTC() command is not recognized by this compiler. Anyone can help me on this. Thank you.

Pam April 19, 2005 12:55

Re: Elapsed time
I forgot to say LINUX is the OS of my computer, if important.

Harish April 19, 2005 13:35

Re: Elapsed time
use dtime() at the beginning and ending of your code and subtract the results.


Pam April 20, 2005 08:13

Re: Elapsed time
Thanks, but g77 does not DTIME() command either. Any clue?

Tom April 20, 2005 09:26

Re: Elapsed time

It is a rather dirty work around, but can't you include MPI? MPI has some subroutines for performance measurements.


Tom (different one) April 20, 2005 10:43

Re: Elapsed time
If your program is called myprog type

time myprog

This will print out the elapsed cpu time (user and system).

Harish April 20, 2005 11:05

Re: Elapsed time
Compile the code as g77 -ff90 ..........

Try etime() if dtime() does not work


Pam April 20, 2005 11:10

Re: Elapsed time
Tom, how can I get this subroutine?

Pam April 20, 2005 11:19

Re: Elapsed time
Thanks Tom. It worked, but I thought that there would be a more sophosticated way to do this.

Tom (different one) April 20, 2005 12:07

Re: Elapsed time
Another way of doing this is to use, assuming your version of g77 supports it, the f90 subroutine cpu_time; e.g. add the line

call cpu_time(start_time)

at the start of the code and

call cpu_time(end_time)

print*,'cpu time is ',end_time-cpu_time

at the end of your code then compile with the -ff90 option. The cpu_time from this should be the same as that printed under user time as I suggested in my earlier post.

Pam April 20, 2005 13:08

Re: Elapsed time
I added this and tried to compile my program as:

g77 -ff90 myprog myprog.f and also g77 -o -ff90 myprog myprog.f

but the message is:

Invalid decleration of or reference to symbol 'cpu_time' , which means the subroutine "cpu_time" cannot be found. Have you ever tried yourself this with g77 ? And linux OS?


Tom April 20, 2005 14:10

Re: Elapsed time
Yes (I tried it before posting). The problem is likely to be that you've got an old version of g77/gcc installed on your computer. I've just tried it again and it actually works without the -ff90 flag on my home machine! Check the version of your compiler (type g77 --version) mine is 3.3.4

You can download the latest version of g77/gcc (you need to be root to install it) form

You could also try replacing, for example, "call cpu_time(x)" by by "call second(x)"

Pam April 20, 2005 18:56

Re: Elapsed time
The version of mine is 3.3.1 (linux). I will try the new version asap, but in the meantime, I tries this second(x). It worked but it is not the equivalent to "time myprog" ie. elapsed time, it just gives "cpu time" times 1e-4.

Tom (different one) April 21, 2005 04:43

Re: Elapsed time
seconds and cpu_time are supposed to be the same function so I'm not too sure why it's not giving the right answer? Also, now I'm back at work I've tested the commands on our version of g77 (gcc 3.2.2 - so it appears to be older than yours!).

You could also try downloading g95 or gfortran which gives you access to other f90 programming features as well as cpu_time (for noncommercial use you can also download the intel f90 compiler).

Mani April 22, 2005 22:06

Re: Elapsed time
The easiest way would be to use a Fortran90 compiler. However, if you need to use g77 and nothing in fortran seems to work, there is a very simple way to make it work with a C function called from fortran. Here's a short example:

///////////////////////////////////////////// // paste into file ctim.c and compile by // gcc -c ctim.c /////////////////////////////////////////////

#include <sys/time.h> #include <sys/resource.h>

double ctim_(void) { struct rusage rusage; double utime,stime;

getrusage(0,&rusage); utime = (rusage.ru_utime.tv_sec

+1e-6*rusage.ru_utime.tv_usec); stime = (rusage.ru_stime.tv_sec

+1e-6*rusage.ru_stime.tv_usec); return(utime +stime); }


ccccccccccccccccccccccccccccccccccccccccccccc c paste into file test.f and compile by c g77 test.f ctim.o ccccccccccccccccccccccccccccccccccccccccccccc

program test

implicit none

integer i,j

double precision x,t,ctim

external ctim

t = ctim()

x = 0d0

do i=1,10000

do j=1,10000




t = ctim()

print *,t,' sec'


Mani April 22, 2005 22:09

Re: Elapsed time
what a mess! got mangled. let me know if you want the files.

Pam April 23, 2005 10:44

Re: Elapsed time
Thank you. Would you send the files to

Regards. Pam

All times are GMT -4. The time now is 20:16.