FYI

John Sh

Member
Local time
Tomorrow, 05:52
Joined
Feb 8, 2021
Messages
535
I've been a bit curious about the time taken for various ways of calling a sub / function where a particular form is involved.
I've tested this over 10000000 iterations with the results below.

In the first test I passed the form name as "Me"
Code:
Private Sub timeTest()
    Dim nNum As Long
    nStart As Double
    nNum = 1
    nStart = timeGetTime
    Do
        nNum = addone(nNum, Me)
    Loop While nNum < 10000000
    Me.txtname = timeGetTime - nStart
End Sub

Private Function addone(nNum As Long, frm As Form) As Long
    addone = nNum + 1
End Function

It took 1.85 seconds to run.

In the second test I used "Screen.activeform"
Code:
Private Sub timeTest()
    Dim nNum As Long
    nStart As Double
    nNum = 1
    nStart = timeGetTime
    Do
        nNum = addone(nNum)
    Loop While nNum < 10000000
    Me.txtname = timeGetTime - nStart
End Sub

Private Function addone(nNum As Long) As Long
    Dim frm As Form
    Set frm = Screen.ActiveForm
    addone = nNum + 1
End Function

This took 39.5 seconds to run, that is about 21 times slower than passing "Me".
As a matter of interest, I tried the first test with While / Wend and it took 1.9 seconds.
Just thought I'd let you know.
 
Last edited:
You have a bit of an apples and oranges comparison.

addone() is different in each case.

In your second test you are creating a n new form reference on each iteration, in the first you re-use the same form reference

Why not keep it the same in both tests?

Something like:
Code:
Private Sub timeTestMe()
    Dim nNum As Long, _
        nStart As Double
    nNum = 1
    nStart = timeGetTime
    Do
        nNum = addone(nNum, Me)
    Loop While nNum < 10000000
    Me.txtname = timeGetTime - nStart
End Sub

Private Sub timeTestForm()
    Dim nNum As Long, _
        nStart As Double, _
        frmPass As Access.Form
    nNum = 1
    nStart = timeGetTime
    Set frmPass = Forms.YourFormName
    Do
        nNum = addone(nNum, fromPass)
    Loop While nNum < 10000000
    Me.txtname = timeGetTime - nStart
End Sub

Private Function addone(nNum As Long, frm As Form) As Long
    addone = nNum + 1
End Function

Also, you really should by now have Option Explicit declared at the top of every code module
 
You have a bit of an apples and oranges comparison.

Why not keep it the same in both tests?

Also, you really should by now have Option Explicit declared at the top of every code module
<You have a bit of an apples and oranges comparison.
Why not keep it the same in both tests?>
If you run two test with the same data / procedure one would surely expect to get the same result, making the second test somewhat redundant.
The whole purpose of the test was to see the difference in time between the two methods, pass the form as "Me" or get the form using "Screen.activeform". This was pointed out at the top of each code segment. If you have a proper look you will see that the "timeTest" call to "AddOne" is also different in both tests.

<Also, you really should by now have Option Explicit declared at the top of every code module>
Assuming something is a good way to make ones self look foolish. I do, in fact, have "Option explicit" at the top of every procedure and module.
It is not necessary to have it at the top of every sub or function!

"Look ---- before you leap"
 
<You have a bit of an apples and oranges comparison.
Why not keep it the same in both tests?>
If you run two test with the same data / procedure one would surely expect to get the same result, making the second test somewhat redundant.
The whole purpose of the test was to see the difference in time between the two methods, pass the form as "Me" or get the form using "Screen.activeform". This was pointed out at the top of each code segment. If you have a proper look you will see that the "timeTest" call to "AddOne" is also different in both tests.

<Also, you really should by now have Option Explicit declared at the top of every code module>
Assuming something is a good way to make ones self look foolish. I do, in fact, have "Option explicit" at the top of every procedure and module.
It is not necessary to have it at the top of every sub or function!

"Look ---- before you leap"
I think you didn't understand what @cheekybuddha is telling you.
Read his post one more time. But a little more carefully this time.

In your second test, you have set a variable to an object 10 million times.
Set frm = Screen.ActiveForm

But in your first test you haven't set this variable at all.
Do you really think running 10 million times a line of code (setting an object), should be equal to not running it at all?
 
Last edited:
I think this is a fairer test...
Code:
Private Sub timeTest()
    Const MAX_L As Long = 10000000
    Dim i As Long
    Dim clock As Single
    Dim frm As Form
    
    clock = Timer
    For i = 0 To MAX_L
        Set frm = Me
    Next
    Debug.Print "Using Me:  ", Round(Timer - clock, 6)
    
    clock = Timer
    For i = 0 To MAX_L
        Set frm = Screen.ActiveForm
    Next
    Debug.Print "Using Screen: ", Round(Timer - clock, 6)
End Sub
...but it yields a similar result...
Code:
Using Me:      0.683594
Using Screen:                18.30469
Also, I think the time cost is in resolving Screen.ActiveForm, not in passing an object variable to a subroutine.
 
But another thing: Me (in this context) and Screen.ActiveForm are UI elements, and there's not a lot of UI operations requiring 10 million iterations. If you perform either of these operations once, or maybe even five or eight times, which is what you would do presenting your UI, the time difference will not affect the user experience.
 
But another thing: Me (in this context) and Screen.ActiveForm are UI elements, and there's not a lot of UI operations requiring 10 million iterations. If you perform either of these operations once, or maybe even five or eight times, which is what you would do presenting your UI, the time difference will not affect the user experience.
But I wanted to know and running the test a handful of times showed no result.
 
But another thing: Me (in this context) and Screen.ActiveForm are UI elements, and there's not a lot of UI operations requiring 10 million iterations. If you perform either of these operations once, or maybe even five or eight times, which is what you would do presenting your UI, the time difference will not affect the user experience.
I didn't want to mention it. But now that we're on details:

I tested your code for 100 times instead of 10 million and deleted the round function to see the exact time.

Even for 100 times, the result was as following:
Using Me: 0
Using Screen: 0

and for 1000 times:
Using Me: 0
Using Screen: 0.0078125

And who writes a code to call a form for a thousand times and how much 0 and 0.007 seconds makes a difference.
 
Last edited:
<I tested your code for 100 times instead of 10 million and deleted the round function to see the exact time.>
There was no "Round" function, timeGetTime records in milliseconds and I rounded it.

<And who writes a code to call a form for a thousand times and how much 0 and 0.007 seconds makes a difference.>
As stated I wanted to know.

It would seem that some of you people have nothing better to do than argue over frivolous points just for the heck of it.
Next time I run a test for my own information, I will keep it to myself.

Post what you like from here on in, I am no longer watching this thread.
John
 
There was no "Round" function, timeGetTime records in milliseconds and I rounded it.
You may want to be more careful about the replies. My comment was a reply to @MarkK's. Not you.

It would seem that some of you people have nothing better to do than argue over frivolous points just for the heck of it.
I didn't know some short tempered members can't stand arguments.
I believe these forums are a place to discuss and exchanging ideas and beliefs, not just saying "Yes, your are correct."
If you're so offended by someone sharing his idea about your posts, then make it clear in your first post by adding : Don't reply bellow.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom