Select Case expressions

New to FreeBASIC? Post your questions here.
fzabkar
Posts: 48
Joined: Sep 29, 2018 2:52

Select Case expressions

Postby fzabkar » Sep 06, 2020 10:53

How can I improve the coding of Case Else? I can't seem to find a suitable example in the FB docs, assuming there is one.

Code: Select all

Dim bfilptr As UByte Ptr

Select Case bfilptr[i]

Case 48 To 64   :   bfilptr[i] -= 6
Case 35         :   bfilptr[i] = 59
Case 65         :   bfilptr[i] = 32
Case 66         :   bfilptr[i] = 63
Case Is > 98    :   bfilptr[i] -= 5
Case Else

    If ( bfilptr[i] <> 10 ) AndAlso ( bfilptr[i] <> 32 ) AndAlso ( bfilptr[i] <> 46 ) Then
       
        bfilptr[i] -= 9
       
    End If

End Select
PaulSquires
Posts: 882
Joined: Jul 14, 2005 23:41

Re: Select Case expressions

Postby PaulSquires » Sep 06, 2020 11:10

How about this (basically, do nothing if the values are 10, 32, 46)

Code: Select all

Select Case bfilptr[i]

Case 48 To 64   :   bfilptr[i] -= 6
Case 35         :   bfilptr[i] = 59
Case 65         :   bfilptr[i] = 32
Case 66         :   bfilptr[i] = 63
Case Is > 98    :   bfilptr[i] -= 5
Case 10, 32, 46
Case Else
        bfilptr[i] -= 9
End Select
fzabkar
Posts: 48
Joined: Sep 29, 2018 2:52

Re: Select Case expressions

Postby fzabkar » Sep 06, 2020 11:32

Many thanks. In fact the same answer occurred to me a couple of minutes ago.
D.J.Peters
Posts: 8169
Joined: May 28, 2005 3:28
Contact:

Re: Select Case expressions

Postby D.J.Peters » Sep 06, 2020 12:34

"case as const" will speed it up !
(internal a fast jump table are builded)

Joshy

Code: Select all

if bfilptr[i]>98 then
  bfilptr[i] -= 5
else 
  select case as const bfilptr[i]
  case 10
  case 32
  case 35         :   bfilptr[i] = 59
  case 46
  case 48 To 64   :   bfilptr[i] -= 6
  case 65         :   bfilptr[i] = 32
  case 66         :   bfilptr[i] = 63
  case else
    bfilptr[i] -= 9
  end select
end if 
fzabkar
Posts: 48
Joined: Sep 29, 2018 2:52

Re: Select Case expressions

Postby fzabkar » Sep 09, 2020 5:44

What about this?

Code: Select all

  Select Case As Const bfilptr[i]
  Case 10
  Case 32
  Case 35         :   bfilptr[i] = 59
  Case 46
  Case 48 To 64   :   bfilptr[i] -= 6
  Case 65         :   bfilptr[i] = 32
  Case 66         :   bfilptr[i] = 63
  Case 99 To 255  :   bfilptr[i] -= 5
  Case Else       :   bfilptr[i] -= 9
  End Select

Is there any reason why I can't put 10, 32, 46 on the same line?
MrSwiss
Posts: 3605
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Select Case expressions

Postby MrSwiss » Sep 09, 2020 14:31

fzabkar wrote:What about this?
More than one 'case' on the same line:

Code: Select all

Select Case As Const bfilptr[i]
  Case 10, 32, 46   ' do nothing
  Case 35         :   bfilptr[i] = 59
  Case 48 To 64   :   bfilptr[i] -= 6
  Case 65         :   bfilptr[i] = 32
  Case 66         :   bfilptr[i] = 63
  Case 99 To 255  :   bfilptr[i] -= 5
  Case Else       :   bfilptr[i] -= 9
End Select
fzabkar
Posts: 48
Joined: Sep 29, 2018 2:52

Re: Select Case expressions

Postby fzabkar » Sep 09, 2020 19:08

MrSwiss wrote:
fzabkar wrote:What about this?
More than one 'case' on the same line:

I was wondering whether separate lines, or the numerical order of the constants, affected the optimisation of the jump table.
MrSwiss
Posts: 3605
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Select Case expressions

Postby MrSwiss » Sep 09, 2020 19:25

fzabkar wrote:I was wondering whether separate lines, or the numerical order of the constants, affected the optimisation of the jump table.

No, it doesn't. Since you'll not know which constant is called the most.
The jump table itself optimizes the Select Case [As Const] statement.

BTW: max. size of jumptable is: 8K entries (current FBC)

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 4 guests