• Welcome to Jose's Read Only Forum 2023.
 

Tip - SELECT CASE AS LONG for string comparisons

Started by Bernard Ertl, May 21, 2007, 11:39:08 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Bernard Ertl

SELECT CASE AS LONG works faster than SELECT CASE sVar$:#COMPILE EXE
#DIM ALL

%MaxIterations = 90000&

FUNCTION PBMAIN()

   LOCAL I AS LONG
   LOCAL fdStart AS DOUBLE, fdEnd AS DOUBLE
   LOCAL sArray() AS STRING
   LOCAL qDataValue AS QUAD, sData AS STRING

   DIM sArray( 2)

   sArray( 0) = "i"
   sArray( 1) = "f"
   sArray( 2) = "s"

   sData = MKQ$( 2342353)

   fdStart = TIMER
   FOR I = 1 TO %MaxIterations
      SELECT CASE AS LONG ASC( sArray( I MOD 4))
         CASE 105 : qDataValue = ASC(sData)
         CASE 102 : qDataValue = CVI(sData)
         CASE 115 : qDataValue = CVL(sData)
      END SELECT
   NEXT
   fdEnd = TIMER

   MSGBOX "AS LONG took " + FORMAT$( fdEnd - fdStart) + " seconds."

   fdStart = TIMER
   FOR I = 1 TO %MaxIterations
      SELECT CASE sArray( I MOD 4)
         CASE "i" : qDataValue = ASC(sData)
         CASE "f" : qDataValue = CVI(sData)
         CASE "s" : qDataValue = CVL(sData)
      END SELECT
   NEXT
   fdEnd = TIMER

   MSGBOX "AS char took " + FORMAT$( fdEnd - fdStart) + " seconds."

END FUNCTION

Theo Gottwald

I am using this for long time.
There is even the chance to get another (small) improvement, if you replace the ASC() with a PEEK(STRPTR()).

Another sidenote for reader not familiar with Powerbasic.
When using the ASC, Powerbasic has another feature that is unusal somewhere else.
That is, it has an implicit MID$(), inside the ASC, if you write:

A$=ASC(X$,21)

Means he takes Byte 21 from X$ and gives you the ASC-Value of this.
The only small Problem with this is, that the finaly compiled ASM is slwer then if you do the same with PEEK(STRPTR(X+...)).