Optimize a simple code for performance

Hi,

Thanks for the tips :slight_smile: I noticed the bug once posted, but that was actually a bad
copy-paste from my more elaborated and working code, where I had to do much cleaning and apparently did too much. The original algorithm was coded in fortran many years ago and has been flawlessly working since then, with no bugs :slight_smile:

Regarding the question itself, I actually didn’t want to use a different form for the laplacian function in 1D, 2D or 3D. I just wanted to use multiple dispatch to decide the case. Actually I use defined user-defined types for that

abstract type USER_DEFINED_TYPE end;

abstract type Numerical_Derivatives <: USER_DEFINED_TYPE end;

struct Hard_End_Points <: Numerical_Derivatives end;
struct Cartesian_Hard_End_Points    <: Numerical_Derivatives end;    
struct Cartesian_Hard_End_Points_1D <: Numerical_Derivatives end;    
struct Cartesian_Hard_End_Points_2D <: Numerical_Derivatives end;    
struct Cartesian_Hard_End_Points_3D <: Numerical_Derivatives end;    

so that the laplacian function is always called the same way

Lap(d2f,grid,dr,calc)

with

calc=Cartesian_Hard_Hend_Points_1D()

or

calc=Cartesian_Hard_Hend_Points_2D() 

etc. In this way (though I did not say anything like that before), adding a new parameter j as in your Laplacian_1D! would break that. More importantly, your version does not do what mine does. Your version requires the array d2y in Laplacian_1D! to be initialised to 0, or otherwise it will add the result of the laplacian to the already stored values in d2y. I say this because I already found myself these two improvements, but realised that this was not what the original Lapla routine is doing…

In any case, more that specifically optimising this, I believe the question is more general, regarding performance penalty when trying to write a function acting on arrays (as Laplacian_2D!) vs acting on individual elements as Laplacian_1D! or Lapla do). I can understand that a little of overhead must be there, but I was expecting that one could make the time of executions of both versions could be essentially the same -which is not in the present case.
I mean, maybe I can re-think the whole thing and code it in a clever but anti-natural way, but I’m not really interested on that, I believe that coding the laplacian of a 2D function in a 2D array is the most natural way of write things… not the smartest probably, but the simplest one.

Thanks a lot for your kind help :slight_smile:

Ferran.