Fastest location for a method

Potentially a very dumb question;
Is a sub or method placed locally on a form any faster than say a method off-form? The development interface of any language creates the illusion that a method in a module folder is further away from the form and thus harder to access.

Doubt that it is a dumb question at all :wink:

Quickly put together a test app and tested this with the profiler. It seems that a method located on the form is slightly faster (+/-1ms) than the same method located in a module.

I suspect though that placing all your methods in a form for such a slight increase in speed might not always be the best design decision from a re-usability perspective.

This is the code I used inside the methods for testing:

  dim i as integer
  dim a as integer
  
  for i = 1 to 100000
    a = 123 + 4
  next i 

Chris, I’m curious why it matters? Unless you are writing an app that absolutely depends on the best speed, I doubt anyone would notice any difference anyway.

Thanks for the helpful commentary alwyn. I had a theory that the compiler crunched everything into a stack with event and conditional branches, thus negating the perceived distance problem. I guess that a 1 MS penalty for an off form method is no big deal unless you find a silly way to amplify this penalty several thousand times.

Kem, As stated it was a potentially dumb question and I guess that it doesnt really matter in the big scheme of writing good code. Like many of the very helpful people on this forum, I came from a very primitive coding background (radio shack TRS-80!!) where every bit of efficiency mattered when you were trying to do something with 16K of RAM.

It’s mind-blowing to think that you now virtually tuck away the size and speed of a TRS-80 (or the Apple ][ or the like) in a space that doesn’t even amount to a corner of a modern machine. My first hard drive was an external I bought for an SE/30, it cost $700 and was 120 MB. Today it wouldn’t house more than a few files at best, and an emulator would have to slowed dramatically to match that machine’s speed. :slight_smile:

If I understood @Joe Ranieri correctly at XDC, a module is the “fastest” location because there are no (or fewer) lookups involved, but as Alwyn implied (and I’ll say flat-out), that’s an awful way to code. Still, if speed is the critical factor, you do what you must.

I fear I might have started the avalanche of commentary on hardware nostalgia. I am still haunted by the restrictions of yesteryear
even though computing (speed,storage, processing efficiency) has advanced way beyond my change in attitude.

This is what I measure in microseconds for 10 million calls, bar() is on the window, foo() in a Module.

[code]no pragmas

  • bar: 596982.546 foo: 458578.513 foo 25% faster
  • bar: 595107.192 foo: 458964.000
  • bar: 608073.809 foo: 458002.308
  • bar: 609879.122 foo: 459412.310

with pragmas

  • bar: 309376.349 foo: 126653.129 foo 60% faster
  • bar: 309728.208 foo: 126437.386
  • bar: 309232.224 foo: 126618.675
  • bar: 307648.769 foo: 126422.103

Sub Action()
#pragma DisableBackgroundTasks
#pragma DisableBoundsChecking
#pragma NilObjectChecking false
#pragma StackOverflowChecking false

dim t0, t1, t2, t3 As double, i, last As integer

last = 10000000

t0 = Microseconds
for i = 0 to last
bar
next
t1 = Microseconds

t2 = Microseconds
for i = 0 to last
foo
next
t3 = Microseconds

ta.AppendText ("- bar: " + Format(t1-t0, “0.000”) + " foo: " + Format(t3-t2, “0.000”)) + EndOfLine
End Sub

Sub bar() //in Window
End Sub

Sub foo() //in Module
End Sub[/code]

Wow!!! 120mb!! my first HDD was a 40mb seagate voice coil. Notice the difference between talking fishing and computer technology. With fishing stories you are always bragging how big everything is. With computers, we tend to brag about how small everything is. th opposite to sex really.

Will, you tested the compiled app?

Runned my test app again and are still getting faster calls from a method located on a window when compared to same method located inside a module? Hmmm, perhaps running in debug mode instead of compiled mode affects the accuracy of the results…

120MB definitely wasn’t too shabby for a first hard drive.

My first modem was a whopping 1200 baud too. But that was for my Apple ][+. :slight_smile:

Kem, with that kind of lightning speed, you could download a movie in 4-5 months

Yeah, the days of ASCII interfaces.

I remember a 100KB image taking at least 45 minutes to download with a 2400 baud connection. And when you used an advanced protocol like ZModem you could even continue with the download if the connection got interrupted when someone tried to make a voice call, while you were connected, in the other part of the house on the same line.

Yes. In debug times were longer overall but similar ratios.

I used to love how your favourite pirated app from the local BBS would download 99% and then drop the line. I think that it was a conspiracy to turn clean living people into foul mouthed, red eyed maniacs

LOL,

Even worse was when your game/app spanned 40 stiffy disks, with disk 38 having a bad sector… but you were lucky enough to be made aware of this fact after the install reached disk 38.

Ah, the memories…

It was all run by little old ladies in dusty cupboards - see https://www.youtube.com/watch?v=dTos3CsvnLQ for ‘proof’…

20 MB @ $800 - I was an early adopter :slight_smile: