tags:

views:

83

answers:

4

Which is the most efficient way to do this?

Sub pvaSetWeek(Optional weekOffset As Long = 0)
    Dim theDayToday As Long

    theDayToday = Weekday(Now, vbMonday)

    'Set start to Monday
    Range("pvaStartDate") = Int(Now) - (theDayToday - 1) - (weekOffset * 7)

    'Set end to Sunday
    Range("pvaEndDate") = Int(Now) + (7 - theDayToday) - (weekOffset * 7)
End Sub

or

Sub pvaSetWeek(Optional weekOffset As Long = 0)    
    'Set start to Monday
    Range("pvaStartDate") = Int(Now) - (Weekday(Now, vbMonday)- 1) - (weekOffset * 7)

    'Set end to Sunday
    Range("pvaEndDate") = Int(Now) + (7 - Weekday(Now, vbMonday)) - (weekOffset * 7)
End Sub

And why?

Edit to add: I usually go with the first way as it is easier to read/debug and obviously scales better if the same value needs used more than a couple of times, but where the value may only be used a few times I've often wondered if there is any penalty, however small, for doing things one way or another.

+1  A: 

The first method is certainly better in terms of readability. I would prefer it.

In my opinion, one variable assignment will not be a noticeable overhead. Plus, the fact that you are writing VBA and not some time / mission critical close-to-the-processor kind of assembly code, means that your biggest worry should be the maintainability of code and not miniscule theoretical computation time efficiencies.

Crimson
I appreciate the difference, if any, would literally be unmeasurable, but things like this keep me awake at night ;)
Lunatik
:) - I simply wanted a smiley here but SO stopped me..
Crimson
By using VBA, you have already thrown all computation time efficiencies out the window.
Justice
A: 

This may seem to be a trite answer... but have you tried timing it? If there is no "significant difference"* then work out which is more readable/maintainable.

** i'll leave that to you.

Mark Nold
A: 

I think some "refactoring" is available. How about this?

Sub pvaSetWeek(Optional weekOffset As Long = 0)  
  Dim TheDateToday as byte, lngRangeBase as Long, lngRangeStart As Long, lngRangeEnd As Long  
  TheDateToday = Weekday(Now, vbMonday)  
  lngRangeBase = (weekOffset * 7)  

  lngRangeStart =  - ((theDayToday - 1) - lngRangeBase )  
  lngRangeEnd =  (7 - theDayToday) - lngRangeBase  

  'Set start to Monday  
  Range("pvaStartDate") = Int(Now) + lngRangeStart  
  'Set end to Sunday  
  Range("pvaEndDate") = Int(Now) + lngRangeEnd  
End Sub
Smandoli
+2  A: 

Other answers are good.

I would just like to point out that SO contains many many questions of the form "Is X more efficient than Y?", like "Is ++i more efficient than i++?" or "Is inlining function calls more efficient than not?". The answer is, most often, as some contributor pointed out, like "getting a haircut to lose weight".

Sure it might actually make a difference, but don't assume it makes a significant difference until you prove that it does in your case.

Mike Dunlavey
+1 but the readability argument *does* hold sway
annakata
@annakata: You're right, but I did a lot of thinking about this some years ago, and convinced myself that readability for its own sake is not useful, but readability serving maintainability is where the real value is.
Mike Dunlavey
Like the haircut analogy, even if it wasn't originally yours. Here, have 25 rep ;)
Lunatik
@Lunatik: Isn't that a great metaphor? I think I'm the first one who saw it appear on SO in a comment, from a new user, but my forgettery quickly forgot who it was.
Mike Dunlavey