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
#2
@TerryRitchie I don't know if you have a tutorial lesson on these two commands, or not.  If not, you might want to consider adding one (or highlighting the speed difference in your existing one, if it exists), so folks will get more familiar with using ASC and its extended syntax like this.  It really is a LOT faster than using MID$, when you don't have to!  Wink
Reply
#3
   


Your computer is faster than mine.
Reply
#4
(10-22-2023, 02:05 PM)Dimster Wrote: Your computer is faster than mine.

Still about the difference I'd expect on the ratio for performance between MID$ and ASC.  A 1000% speed boost isn't anything to sneeze over!  Wink
Reply
#5
(10-22-2023, 03:21 AM)SMcNeill Wrote: @TerryRitchie I don't know if you have a tutorial lesson on these two commands, or not.  If not, you might want to consider adding one (or highlighting the speed difference in your existing one, if it exists), so folks will get more familiar with using ASC and its extended syntax like this.  It really is a LOT faster than using MID$, when you don't have to!  Wink
I have both ASC and MID$ in Lesson 9 but I just checked and I don't have the extended syntax for ASC detailed. I'll make sure to add that shortly.

I've been scarce the past few days because I'm once again redoing the entire tutorial site. I had all the code listings embedded in the pages but because I can't trust that method now I'm converting the code listings over to pure HTML. I'm currently up to chapter 10.

I found a way to use the Export to HTML feature in the IDE and still retain the format while incorporating the listing in HTML. It takes longer than embedding but it will guarantee the listings are also available.

I have a feeling SquareSpace is not going to be much help unless I pay their monthly fee. When I asked them about embedding not working all I got was a canned response and of course information on how to become a "full" SquareSpace member. Thank you Google for once again abandoning another one of your projects.
There are two ways to write error-free programs; only the third one works.
QB64 Tutorial
Reply
#6
I had no idea you could even do replacements with ASC but maybe I will from now on. Seems fairly handy.
Tread on those who tread on you

Reply
#7
(10-22-2023, 03:21 AM)SMcNeill Wrote: @TerryRitchie I don't know if you have a tutorial lesson on these two commands, or not.  If not, you might want to consider adding one (or highlighting the speed difference in your existing one, if it exists), so folks will get more familiar with using ASC and its extended syntax like this.  It really is a LOT faster than using MID$, when you don't have to!  Wink

I updated Lesson 9 in the tutorial with changes. Thanks for the head's up Steve.

A lot of the tutorial I wrote back in 2013. Many new features have come along since (especially with pe) so I need to go through the tutorial and see what else may be missing.
There are two ways to write error-free programs; only the third one works.
QB64 Tutorial
Reply
#8
4.066 sec vs 0.483 sec


Vector GP66 gaming laptop. Not shabby, good tip.
It's not the having, it's the doing.
Reply
#9
All well and good for replacing single characters but not so useful I suspect for replacing more than one character at a time e.g. Replace 'Art' with 'Tar' or 'Rat'.

FWIW, ~13 seconds for MID$ and ~1 second for ASC.

TR
Reply
#10
I've generally defaulted to the use of MID$. I was aware that ASC existed, but didn't make much use of the knowledge. Good stuff to remember.

My machine:
6.979 MID$
0.605 ASC
DO: LOOP: DO: LOOP
sha_na_na_na_na_na_na_na_na_na:
Reply




Users browsing this thread: 1 Guest(s)