Discussion:
About 'use' statement usage
Angelo Graziosi
2018-09-06 16:00:58 UTC
Permalink
Often a module calls 'elements' (variables, functions, subroutines etc.) from other modules. The 'called' module can be 'used' once, at the beginning of the 'calling' module or repeated for each function/subroutine 'using' it. In short,

CASE 1
======

module foo
use bar, only: bar0, bar1, bar2,...

! using bar0
... bar0 ...

contains

subroutine sub_bar1()
...
... bar1 ...
end sub...

subroutine sub_bar2()
...
... bar2 ...
end sub...

...
end module foo


CASE 2
======

module foo
use bar, only: bar0

! using bar0
... bar0 ...

contains

subroutine sub_bar1()
use bar, only: bar1
...
... bar1 ...
end sub...

subroutine sub_bar2()
use bar, only: bar2
...
... bar2 ...
end sub...

...
end module foo


Which is better? CASE1 or CASE 2?

TIA.

Ciao,
Angelo.
Janne Blomqvist
2018-09-06 18:45:14 UTC
Permalink
Post by Angelo Graziosi
Which is better? CASE1 or CASE 2?
Better in what way?

I'd say from a code clarity point of view, case 2 is better.

From a performance perspective, I'd guess case 1 is better, at least with
the current module implementation in GFortran.
--
Janne Blomqvist
Angelo Graziosi
2018-09-06 19:50:37 UTC
Permalink
Post by Janne Blomqvist
Post by Angelo Graziosi
Which is better? CASE1 or CASE 2?
Better in what way?
I'd say from a code clarity point of view, case 2 is better.
From a performance perspective, I'd guess case 1 is better, at least with
the current module implementation in GFortran.
Thanks Janne, this confirms what I thought.

In CASE 2 we can see "who needs what" while CASE 1 should be faster, at least in compilation...

Thanks a lot for clarification.

Ciao,
Angelo.

Loading...