Author Topic: Tip - SELECT CASE AS LONG for string comparisons  (Read 4291 times)

0 Members and 1 Guest are viewing this topic.

Offline Bernard Ertl

  • Newbie
  • *
  • Posts: 14
    • InterPlan Systems
Tip - SELECT CASE AS LONG for string comparisons
« 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

Offline Theo Gottwald

  • Administrator
  • Hero Member
  • *****
  • Posts: 918
    • it-berater
Re: Tip - SELECT CASE AS LONG for string comparisons
« Reply #1 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+...)).