Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Which way is faster? 1

Status
Not open for further replies.

SkennyR

Programmer
Mar 7, 2004
157
US
I am polling my printer port for inputs 10,11,12,13, and 15.

I was wondering which method would be faster?
This one:
_____________________________________________
Global pin(5) as boolean
Dim x as integer

'sub to read printer port
For x = 0 To 5: pin(x) = False: Next x
'code to read port and set pin(X) true or false
_____________________________________________

OR this one:

_____________________________________________
Global pin() as boolean
Dim x as integer

'sub to read printer port
Redim pin(5)
'code to read port and set pin(X) true or false
_____________________________________________

Thanks
 
Second method will be slower because each Redim statement destroys the existing array and creates a new array at a different memory location.

However, there is another method, which is way faster than both of these.
[tt]
Global pin(5) as boolean
'sub to read printer port
Erase pin
'code to read port and set pin(X) true or false
[/tt]
Erase statement initializes a fixed length array and destroys a dynamic/variable length array.

I am also wondering if you read the status of each pin and assign its value explicitly to pin() array elements, why do you need to initialize them in the first place? They will be overwritten by new status every time.
 
Thanks Hypetia!
I was thinking that the loop to set all pins false would take more time then redim.
Once again, I am faced with the lack of knowledge I contain.

I am also wondering if you read the status of each pin and assign its value explicitly to pin() array elements, why do you need to initialize them in the first place? They will be overwritten by new status every time.

I am using an IF statement to set the pin() true, based on the printer port input being true.
If the printer port pin goes low, then the pin() would not change back to false.
My first post was misleading (by mistake), it should have read that pin(x) is only set true.
Again thanks!
Whenever I have a VB question I search this forum first, and usually get my answer.
 


SkennyR,

Use your Watch Window to observe what happens when you Erase the array. It will answer your question.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
OK, I haven't used that before, sounds very useful.

Thanks!
 
>I was thinking that the loop to set all pins false would take more time then redim.

I wrote following test code to compare the results.
___
[tt]
Private Sub Form_Load()
Dim A(5) As Boolean
Dim B() As Boolean
Dim J As Long, K As Long, S As Single
Dim Result As String
Const Max = 10000000
S = Timer
For J = 1 To Max
For K = 0 To 5
A(K) = False
Next
Next
Result = "For-Next: " & Timer - S & vbLf

S = Timer
For J = 1 To Max
ReDim B(5)
Next
Result = Result & "ReDim: " & Timer - S & vbLf

S = Timer
For J = 1 To Max
Erase A
Next
Result = Result & "Erase: " & Timer - S

MsgBox Result
Unload Me
End Sub[/tt]
___

I ran the above code twice, once from IDE and again after compiling. The message box output is as below.

From IDE
For-Next: 1.289
ReDim: 2.904998
Erase: 0.7739995

From EXE (Optimized for fast code)
For-Next: 8.800732E-02
ReDim: 2.677995
Erase: 0.6349856

As we see, the For-Next loop improves dramatically after compiling. There is no significant change in other two methods. ReDim method, however, remains the slowest in both cases.

>I am using an IF statement to set the pin() true...
Fair enough.
 
Interesting.
I ran your code and got pretty close to same results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top