Select case block vs array of function pointers

Hi all,

I would like some advice about the following. I have a quite big SELECT CASE block where cases are unique integers. The block includes about 200 cases representing different functions. I was wondering if I could get any benefit in terms of speed by replacing the big SELECT CASE block with an array pointers pointing to functions to apply. Thanks in advance.



@FilippoM ,

Welcome, glad you followed up on my suggestion at the Intel Fortran forum and posted here. As I mentioned on the Intel forum, please also take a look at the comp.lang.fortran thread from a few years ago - you may find some useful pointers (!) there.

Making a selection from 200 things will be so fast why worry about speed? Loading up the chosen function will undoubtable take longer. Ease of programming, understandability and maintenance would be what would determine my choice of method.

At some point, the decision of which function gets called has to be made. You can’t really eliminate the construct, just change where it appears. As @andrew_4619 mentioned, I would make the decision of whether your select case block appears at the call site, or whether it assigns a pointer that gets called later based on what makes the code easier to understand. There’s a lot of context we don’t have that should go into making that decision.

As far as performance is concerned, measure and test. If this decision only needs to happen once, but the function needs called inside some hot loop, using a function pointer may buy you some performance benefit. Otherwise, it’s probably a toss up, but definitely measure to be sure.