Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28495 Discussions

Efficieny of select case with multiple item case

Andrew_Smith
Valued Contributor I
313 Views
I have a select case with maybe 400 cases. I have coded it into groups because I wanted to group selections belonging to function. Here is a simplification:

[bash]select case(i)
case (0:99)
   call selectFirst100(i)
case(100:199)
   call select100To200(i)
case((200:299)
   call select200To300(i)
case((300:399)
   call select300To300(i)
end select

subroutine selectFirst100(i)
   integer, intent(in) :: i

   select case(i)
   case(1)
       !Do something
   case(2)
       !ETC
   end select
end subroutine[/bash]

When I step through this in the VS debugger I notice that the top level select jumps around checking each case in turn until it finds one that matches, but the lower level one with single items in the cases jumps straight to the selection. It seams like a completly different approach it taken for the two cases and I was wondering if the multi-item (top level) selection has a penalty in speed?

Maybe I should do the top level differently like:

[bash]if (i > -1 .and. i < 100) then
   call selectFirst100(i)
else if (i < 200) then
   call select100To200(i)
else if (i < 300) then
   call select200To300(i)
else if (i < 400) then
   call select300To400(i)
end if[/bash]
0 Kudos
1 Reply
Steven_L_Intel1
Employee
313 Views
The compiler does optimize simpler SELECT CASE constructs, but when there is a range, it reverts to a series of comparisons just like your replacement code. I suggest leaving the outer one as it is. Or you might do something like having the outer one select on i/100 with values of 0, 1, 2, etc. That would probably get optimized.
0 Kudos
Reply