Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Speed comparison of MID$ vs ASC
#1
As it seems everyone is always trying to make their stuff run as fast as possible, I want to take a moment to once again point out the difference in speed between using STRING manipulation (such as MID$) verses the equivalent numeric manipulation (such as with ASC).

Take a brief moment to look at the following code, and compare the run times of these two simple routines:

Code: (Select All)
DEFLNG A-Z
CONST Limit = 100000000 '100 million

DIM s AS STRING
s = STRING$(Limit, "A") 'a string Limit characters long of all "A".

t# = TIMER
FOR i = 1 TO Limit
    IF MID$(s, i, 1) = "A" THEN MID$(s, i, 1) = "B"
NEXT
t1# = TIMER
FOR i = 1 TO Limit
    IF ASC(s, i) = 66 THEN ASC(s, i) = 67 'replace "B" with "C"
NEXT
t2# = TIMER

PRINT USING "###.### seconds to replace with MID$."; t1# - t#
PRINT USING "###.### seconds to replace with ASC."; t2# - t1#

PRINT LEFT$(s, 10) 'just to show we're all "C" now, so we know both replacements worked.


Replacing 100 million characters inside a string with another character, so give this a wee bit to run.  (Particularly if you're on an older, slower PC.)   On my machine, this takes about 8 seconds from start to finish, so you can use that as some sort of general starting benchmark of what to expect with a higher-end PC.  (If your machine is 10 years old, or a bargain basement model, give it 30+ seconds to run.  If you're on something older than that, go grab yourself a cup of coffee and kindly report the results when your antique finishes two hours from now -- I'd love to see the comparison values on some older hardware!)

Most folks seem to forget that in QB64PE, you can add a second exponent to the ASC command to get the particular character you want.   Even more folks seem to have absolutely no knowledge in the fact that ASC is also a SUB and not just a FUNCTION, and that the SUB version can be used to assign values to your string!

If you guys are ever the type to be concerned about the speed and performance of your programs, **REMEMBER** these two commands and the features they possess.  They're quite a bit faster than MID$, and can improve program performance considerably!
Reply


Messages In This Thread
Speed comparison of MID$ vs ASC - by SMcNeill - 10-22-2023, 03:19 AM
RE: Speed comparison of MID$ vs ASC - by SMcNeill - 10-22-2023, 03:21 AM
RE: Speed comparison of MID$ vs ASC - by Dimster - 10-22-2023, 02:05 PM
RE: Speed comparison of MID$ vs ASC - by SMcNeill - 10-22-2023, 02:19 PM
RE: Speed comparison of MID$ vs ASC - by OldMoses - 11-03-2023, 11:24 AM



Users browsing this thread: 2 Guest(s)