I am wondering hom much is the efficiency (or speed, CPU time) loose due to using object oreinted programming in fortran?
Does introducing long hierarchies of classes makes the execusion of code slower?
Is using polymorphic pointers costly in terms of slowing down the code execusion?
Here I made a very simple example (please see the attached file)
Consider a simple subroutine which inputs four double precision numbers and multiplies them. I would like to run this subroutine 10**7 times , where the subroutine is introduced differently (in different hierarchies of classes, etc) , and compare the CPU time for this process. Consider 5 cases:
1- Subroutine is intoduced in the main.f90 which is the main project file.
2- I make a class named A and intoduce the subroutine as a type-bound procedure of A.
3- I extend call A to AA and introduce subroutine as type-bound procedure of AA.
4- I introduce a class B which has a polymorphic pointer which points to AA .
5- I introduce the subroutine as a type-bound procedure of B as well. ( this is similar to 2).
I run the code for above cases and measure the execuation time of a loop which calls that subroutine 10**7 times,
here are the results: (on my pc, Core 2 Duo, 32 bit)
1- 2.67 seconds
2- 4.26 seconds
3- 4.27 seconds
4- 3.45 seconds
5- 4.26 seconds
Clearly defining the subroutine as a type-bound procedure slows down the execution performance !!! but is there a way around this cost?? other question is that why 4 is faster than 2, 3 and 5 ?
thank you for your inputs