Why would you use Mid$ when parsing a string? I am not sure what the $ does?
Printable View
Why would you use Mid$ when parsing a string? I am not sure what the $ does?
The $ indicates string. Mid$ only works with strings and Mid also works with other data types. (I'm not sure which others) Therefore, Mid$ is faster.
Back in the day, $ used to indicate a string(text) variable in basic, coincidently, you can still use it to declare a string variable.
The functions are identical in code, the only reason Mid$ is faster is because the variables are explicitly declared as string and therefore don't have to be converted from a variant to whatever which takes that extra few nanoseconds.
Here's the whole deal. :)
I'm almost sure it's correct, however, do some tests because I sort of "forgot" to ;)
But the differences are very small, so it only matters if you use very large strings and/or quantities of strings. :rolleyes:
Code:' Fastest:
Dim A As String
A = Mid$(MyText, X, Y)
' Following two are the same speed:
Dim A As String
A = Mid(MyText, X, Y) ' This is what I always use! :)
Dim A As Variant
A = Mid$(MyText, X, Y)
' Slower:
Dim A As Variant
A = Mid(MyText, X, Y)
' Slowest:
' No Option Explicit and no Dim
A = Mid(MyText, X, Y)
Hehe yonatan, how do you know it's slower to return variant into a variant - because that's what i've been doing if i use Mid, not Mid$. Well, should do some tests, but not on this comp
I think the poor Mid (the one with no dollars ;)) puts a string in a variant, which has to convert back to a variant before it goes to the original variant which was supposed to receive the string.
Is that decipherable?
Maybe I'm just tired. :rolleyes:
eh? convert a variant back to a variant? my thought was that you don't have to convert the variant you get in return but it has to be converted to a string if it's assigned to a string.
Code:$ = String
% = Integer
& = Long
! = Single
# = Double
@ = Currency
(Nothing) = Variant
Shortcuts for declaration of strings
Dim Art$ is the same as Dim Art As String
Dim intCre% is the same as Dim intCre as Integer
etc.
dim Art is the same as Dim Art As Variant
If you don't use Option Explicit in your code all assignments are slower even if you have declared your variables.
This is because of the fact that VB has to check if the variable is declared or not during run-time. If it's not declared VB create it in the form of a Variant.
When you use Option Explicit this check isn't necessary during run-time because it's done during the compile.
If you go to tools/options/editor in VB and tick off Require variable declaration and then you can't forget to put in option explicit