» 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!

Right$ vs Mid$

Right$ seems to be faster, although the last returned value might be reason to argue.
But that's why I always test everything 5 times =o).
I used Mid$ to return the last 10 characters of a line, and Right$ to do the same.

Startup:
strString = "12345678901234567890" '//20 Char

Code:
Public Sub TestOne() Dim strRet As String strRet = Mid$(strString, 10, 10) '//Last 10 chars End Sub Public Sub TestTwo() Dim strRet As String strRet = Right$(strString, 10) '//Last 10 chars End Sub

Mid$ % faster than Right$ Mid$ (sec) Right$ (sec)
25.7% 0.337768 0.268625
24.6% 0.340531 0.273254
23.5% 0.336644 0.272481
26.1% 0.339100 0.268942
6.7% 0.336155 0.315059


User contributed notes:

Author: PRoPHEZZoR (Name_Here_No_Spam at HaCKERMAiL dot COM) Date: 11:08 24/08/2004
Your Mid$() thingy gets 2 values, try Mid$(strString,10) instead.

Author: Javin () Date: 20:08 25/08/2004
It only makes sense to me that passing Mid$ two values would be the appropriate way to do it. By giving it the 10 character flag, it'll know what the "end" of the string will be in advance, without having to figure it out as it goes along.

-Javin

Author: Tom (hure) Date: 14:05 31/05/2005
Yes, but an extra argument adds extra overhead in the form of stack allocation. This test isn't really fair; use the right function for the right job and leave it at that.

Author: Almar () Date: 20:05 31/05/2005
It's just a test, ofcourse it would be logical that right would be faster. :)

Author: Tom (hurendo_kun at hotmail dot com) Date: 15:06 08/06/2005
Right. :)

Author: SuperDre () Date: 16:06 22/06/2006
Well, this shows that Right$ is faster then Mid$, why? in this example you know how long the string is beforehand so you know the last 10 characters start at position 10. But in normal coding you have to do an extra check (which I guess Right$ does also do itself internally), so in normal coding if you use mid$() it would look something like this:

Public Sub TestOne()
Dim strRet As String
Dim lLength As Long

lLength = Len(StrString)
if l > 10 then
strRet = Mid$(strString, l, 10)
else
strRet = strString
end if
End Sub

Author: Almar () Date: 19:06 22/06/2006
You'd be suprised how many people use Mid$() for the same thing as what they could have done with Right$() :).

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