Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 586
Default Which is more efficient?

I have a button that is used to make several calculations using a value from
a textbox (tbxQuantity) in my UserForm. This value could be used 10 to 100
times in one procedure. I was wondering what would be the most efficient way
of writng my equations. When I mean efficient I mean speed. Which is more
efficient?

Sub Calculate()

Dim Qty as Integer

Qty = Val(tbxQuantity)
Price = Cost * MarkUp * Qty

End Sub

or

Sub Calculate()

Price = Cost * MarkUp * Val(tbxQuantity)

End Sub

Thanks in Advance!
--
Cheers,
Ryan
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 915
Default Which is more efficient?

RyanH wrote:
I have a button that is used to make several calculations using a value from
a textbox (tbxQuantity) in my UserForm. This value could be used 10 to 100
times in one procedure. I was wondering what would be the most efficient way
of writng my equations. When I mean efficient I mean speed. Which is more
efficient?

Sub Calculate()

Dim Qty as Integer

Qty = Val(tbxQuantity)
Price = Cost * MarkUp * Qty

End Sub

or

Sub Calculate()

Price = Cost * MarkUp * Val(tbxQuantity)

End Sub

Thanks in Advance!


Just a SWAG...

You're not typing Quantity in the latter example, so there may be some
cost in allocating a variant there. But in 10-100 iterations I can't
imagine there is a significant difference between the two versions.

If you're curious, why not set up an experiment with timing variables?
It could be fun. You might need to expand the number of iterations a few
powers of ten to get meaningful results though.

For added interest, try figuring out the runtime differences declaring
Qty as Integer and Long. You might be surprised.
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 389
Default Which is more efficient?

Out of those options, the 2nd is faster in my simple test. 10,000,000
iterations clocked in approximately .455% faster (1.3174 seconds VS 1.3234
seconds). A few thousand iterations will not be noticeable.


--
Tim Zych
www.higherdata.com
Compare data in Excel and find differences with Workbook Compare
Now with Table Compare for quick table comparisons.


"RyanH" wrote in message
...
I have a button that is used to make several calculations using a value
from
a textbox (tbxQuantity) in my UserForm. This value could be used 10 to
100
times in one procedure. I was wondering what would be the most efficient
way
of writng my equations. When I mean efficient I mean speed. Which is
more
efficient?

Sub Calculate()

Dim Qty as Integer

Qty = Val(tbxQuantity)
Price = Cost * MarkUp * Qty

End Sub

or

Sub Calculate()

Price = Cost * MarkUp * Val(tbxQuantity)

End Sub

Thanks in Advance!
--
Cheers,
Ryan



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 915
Default Which is more efficient?

Tim Zych wrote:
Out of those options, the 2nd is faster in my simple test. 10,000,000
iterations clocked in approximately .455% faster (1.3174 seconds VS 1.3234
seconds). A few thousand iterations will not be noticeable.



I reckon too, "a few million" (^:
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 586
Default Which is more efficient?

How do you do that time study? I would guess something like this. Tell me
if I am correct or if you have a better way.

Sub Calculate()

starttime = Format(Date, "mm.ssss")

'my code here

endtime = Format(Date, "mm.ssss")

End Sub
--
Cheers,
Ryan


"smartin" wrote:

RyanH wrote:
I have a button that is used to make several calculations using a value from
a textbox (tbxQuantity) in my UserForm. This value could be used 10 to 100
times in one procedure. I was wondering what would be the most efficient way
of writng my equations. When I mean efficient I mean speed. Which is more
efficient?

Sub Calculate()

Dim Qty as Integer

Qty = Val(tbxQuantity)
Price = Cost * MarkUp * Qty

End Sub

or

Sub Calculate()

Price = Cost * MarkUp * Val(tbxQuantity)

End Sub

Thanks in Advance!


Just a SWAG...

You're not typing Quantity in the latter example, so there may be some
cost in allocating a variant there. But in 10-100 iterations I can't
imagine there is a significant difference between the two versions.

If you're curious, why not set up an experiment with timing variables?
It could be fun. You might need to expand the number of iterations a few
powers of ten to get meaningful results though.

For added interest, try figuring out the runtime differences declaring
Qty as Integer and Long. You might be surprised.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 915
Default Which is more efficient?

RyanH wrote:
How do you do that time study? I would guess something like this. Tell me
if I am correct or if you have a better way.

Sub Calculate()

starttime = Format(Date, "mm.ssss")

'my code here

endtime = Format(Date, "mm.ssss")

End Sub


For a quick and dirty analysis, I would put

debug.print "start," & now()
for MyLoop = 1 to 100,000,000
' Call Calc
next
debug.print "end," & now()

in whatever procedure calls Calc, but as you can see, outside the loop
that actually calls Calc.

Press Ctrl+G to see the results. Copy'n'Paste into Excel for more
analysis. Repeat several times to satisfaction.
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Which is more efficient?

Which one is easier for you to understand?

Which one is easier for you to modify when (not if) you have to?



RyanH wrote:

I have a button that is used to make several calculations using a value from
a textbox (tbxQuantity) in my UserForm. This value could be used 10 to 100
times in one procedure. I was wondering what would be the most efficient way
of writng my equations. When I mean efficient I mean speed. Which is more
efficient?

Sub Calculate()

Dim Qty as Integer

Qty = Val(tbxQuantity)
Price = Cost * MarkUp * Qty

End Sub

or

Sub Calculate()

Price = Cost * MarkUp * Val(tbxQuantity)

End Sub

Thanks in Advance!
--
Cheers,
Ryan


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
More Efficient IF David Excel Programming 1 September 28th 07 12:23 PM
What is more efficient Brad Excel Discussion (Misc queries) 2 November 20th 06 09:13 PM
Is there a more efficient way to do this? Steve Roberts Excel Programming 1 September 26th 05 05:34 PM
More efficient way? Steph[_3_] Excel Programming 6 June 23rd 04 09:34 PM
Which is more efficient? Norm[_5_] Excel Programming 3 April 2nd 04 04:24 PM


All times are GMT +1. The time now is 09:40 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"