» Home » VB Fibre
Site
News
Files

Visual Basic
Strings
Math
General
Properties
Memory
Methods

Search
Testing

Inline ASM-VB
Strings
Math
General
Memory

Search
Using inline ASM
Submit!

$ Saves you…time

So much effect for so little change of code. These are only the statistics of the "Left" function, but I've also done some tests using "Ucase", "Lcase" and "Trim". All these functions are much faster using the "$" sign after the function. This happens for a simple reason: The functions return a variant without the $ sign. And variants are very slow, you should never use them.

Code Used:

Code:
Public Function TestOne() Dim strReturn As String '//Without string sign strReturn = Left("This is a test", 4) End Function Public Sub TestTwo() Dim strReturn As String '//With string sign ($) strReturn = Left$("This is a test", 4) End Sub

Without $ % faster than With $ Without $ (sec) With $ (sec)
147,6% 0,665861 0,268978
145,3% 0,658749 0,26858
148,9% 0,658076 0,264406
161,1% 0,704659 0,269836
141,6% 0,647323 0,267963


User contributed notes:

Author: Almar Joling () Date: 19:03 01/03/2004
Final test \o/ :)

Author: Test () Date: 22:03 02/03/2005
test

Author: Jamie Hurst (JamieAlexanderHurst at hotmail dot com) Date: 17:05 26/05/2005
I've always used the '$' functions being from a BASIC programming background (old versions which only actually supported '$' functions) however I found myself starting to use non '$' functions recently. Time to break this new and bad habit I think. I did'nt actually know that the '$' functions were faster but now that I do I will definatly continue using my traditional method.

Author: Gregg Morrison (Gregg dot Morrison at GMRAD dot COM) Date: 21:08 21/08/2005
The speed increase is because the Left/Right/Mid functions actually return a String as a *Variant* while Left$/Right$/Mid$ return a string. Variant overhead is greater (thus slower, as well as using more memory) than raw String functions. Even faster if you can use LeftB/RightB/MidB and there are a lot of tricks you can use with these, if you are filling strings with Binary data. E.G. myBinStr = bytArray().

Author: Brian Lai (lpppppl at hotmail dot com) Date: 07:09 03/09/2005
Oi...really cool. Quite cool really. I replaced (with the replace function) all my "left"s to "left$"s. Afterall nothing faster happened to my spell check.

Author: Tom (hurendo_kun at hotmail dot com) Date: 04:09 23/09/2005
Huh. I didn't know there were binary versions. I would assume you lose UNICODE extended characters, but thanks for the info, Gregg.

Add user-note
Author:
E-mail (optional):
Anti spam, please enter 'ok' without quotes:
Comment: