Deprecated: Array and string offset access syntax with curly braces is deprecated in /homepages/21/d38531796/htdocs/jose/smfforum/Sources/Subs.php on line 3825
Print Page - Tip - SELECT CASE AS LONG for string comparisons

Theo's Forum

IT-Berater: Theo Gottwald (IT-Consultant) => Low Level Code Optimization => Low Level Code Optimization PB 9.0 => Topic started by: Bernard Ertl on May 21, 2007, 11:39:08 PM

Title: Tip - SELECT CASE AS LONG for string comparisons
Post by: Bernard Ertl on May 21, 2007, 11:39:08 PM
SELECT CASE AS LONG works faster than SELECT CASE sVar$:
Code: [Select]
#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
Title: Re: Tip - SELECT CASE AS LONG for string comparisons
Post by: Theo Gottwald on May 29, 2007, 06:18:40 PM
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+...)).