Difference between Format and $Format?

Nirious

Registered User.
Local time
Today, 07:18
Joined
Jul 31, 2004
Messages
106
I have noticed several functions come in two ways.
With and without a "$". What's the difference?
 
N,

Seriously, I don't know.

OLD versions of any BASIC strongly type the functions MID$, LEN$, etc. They
were all EXPLICITLY string functions.

In the newer versions, I think that the difference is that if you use Mid() you
are passing a variant. And if you use Mid$, then you are explicitly passing a
string value. There must be a couple of nanoseconds difference I bet.

In general, who cares.

ChrisO ??? Where are those timing benchmarks? (Just kidding - I like those!).

Wayne
 
Could be interesting. :D
 
??? :D

Code:
Option Explicit
Option Compare Text

Public Declare Function timeGetTime Lib "Winmm.dll" () As Long


Public Sub Test()
    Dim lngI      As Long
    Dim lngStart  As Long
    Dim strString As String
    Dim vntString As Variant
    Dim rstTest   As DAO.Recordset
    
    Set rstTest = CurrentDb.OpenRecordset("tblTest", dbOpenDynaset)
        
    lngStart = timeGetTime()
    
    For lngI = 1 To 10000
        rstTest.MoveFirst
        
        Do Until rstTest.EOF
            vntString = rstTest!SomeText
            
            If Left(vntString, 1) = "g" Or _
                Mid(vntString, 30, 1) = "h" Or _
                Right(vntString, 1) = "x" Then
                rstTest.Edit
                rstTest!SomeText = Left(vntString, 10) & _
                                   "ZZZZZZZZZZZZZZZ" & _
                                   Mid(vntString, 26)
                rstTest.Update
            End If
            
            rstTest.MoveNext
        Loop
    
    Next lngI
    
    MsgBox timeGetTime() - lngStart  [color=green]' <<< 6028[/color]

    lngStart = timeGetTime()
    
    For lngI = 1 To 10000
        rstTest.MoveFirst
        
        Do Until rstTest.EOF
            strString = rstTest!SomeText
            
            If Left$(strString, 1) = "g" Or _
                Mid$(strString, 30, 1) = "h" Or _
                Right$(strString, 1) = "x" Then
                rstTest.Edit
                rstTest!SomeText = Left$(strString, 10) & _
                                   "ZZZZZZZZZZZZZZZ" & _
                                   Mid$(strString, 26)
                rstTest.Update
            End If
            
            rstTest.MoveNext
        Loop
    
    Next lngI
    
    MsgBox timeGetTime() - lngStart  [color=green]' <<< 5477[/color]
    
End Sub
??? :(

Edit:
Or about 9 microseconds difference between each Variant and String function on an 850 MHz machine.
Some of the functions are used solely in VBA and some a combination of VBA and recordset reads and writes.
This was done because of a point made in this article: -
Test 1: Use Integers Instead of Variants Whenever Possible?

It seems the Strings win the race over the Variants, but not by much, but the Variants may win if solely used with Jet.

Needs further testing; so if someone has an idea for a test case then please post an exact requirement in A97 and we can all give it a shot.

Regards,
Chris.
 
Last edited:
ChrisO,

Don't want to nitpick, but you called the Mid with the variants and you called
the Mid$ with the string. I thought it was a test of implicit calling.

Just kidding!

I called both with the String type variable and the Mid$ was about 10%
faster. Would have expected that.

Thanks,
Wayne
 
G’day Wayne.

“Don't want to nitpick”…And why not? :D

That’s what timing tests are all about so nitpick all you like. :cool:

I think you already know this but for those that don’t…
I used Mid with Variants because Mid returns a Variant and I used Mid$ with Strings because Mid$ returns a String.

I can’t say that it would satisfy the timing tests but it is a place to start.

A rather strange concept may occur…
To time the slow takes the least time…to time the fast takes the most time.
To look at the large needs a small instrument…to look at the small needs the large.

Just a thought…

Regards,
Chris.
 
Format$ is to be used specifically on a string variable.
Format is to be used on any other usable data type.

Both return strings.

Format$ will always prove that little bit faster as it doesn't have to convert a different data type to a string.
 
“Format$ will always prove that little bit faster as it doesn't have to convert a different data type to a string.”

Not always.

That was the very reason for Wayne to nitpick.

The link I posted states that VBA might spend time type casting any other type to a variant for Jet and that takes time.

My problem is not living with ‘Truths’ but to time them.

This is the very reason that the ‘uninformed’ debate seems to be regurgitated time and time again.

In any absolute or real sense, “this is faster than that”, is crap…it simply needs testing.

Without testing it is a folly of the mind to think we can say “this is faster than that” let along if it is worthwhile.
 

Users who are viewing this thread

Back
Top Bottom