kingofaces
Board Regular
- Joined
- Aug 23, 2010
- Messages
- 68
The following code is for degree-day calculation if anyone is familiar with it:
My issue is that this calculation is being repeated about 40,000 times, and seems a bit slow on some older computers I use it on. I'm wondering if the ElseIf statement can drag down calculation times and if another If or Select statement would be more efficient?
Code:
Function SinDD(Min1, Min2, Max, Base, Ceiling) As Double
'GDD Formulas from Allen 1976
Dim Taverage As Double
Dim W As Double
Dim PTheta1 As Double
Dim PTheta2 As Double
Dim Min As Double
Dim SinhalfDD As Double
Dim n As Integer
For n = 1 To 2
Select Case n
Case Is = 1
Min = Min1
Case Is = 2
Min = Min2
End Select
Taverage = (Max + Min) / 2
W = (Max - Min) / 2
If W = 0 And Max < Base Then
SinDD = 0
ElseIf W = 0 And Max > Ceiling Then
SinDD = 0.5 * (Ceiling - Base)
ElseIf W = 0 And Max <= Ceiling And Max >= Base Then
SinDD = 0.5 * (Taverage - Base)
ElseIf Abs(W) > 0 Then
PTheta1 = (Base - Taverage) / W
PTheta2 = (Ceiling - Taverage) / W
If Abs(PTheta1) < 1 Then
Theta1 = Atn(PTheta1 / Sqr(Abs(1 - PTheta1 ^ 2)))
ElseIf PTheta1 >= 1 Then
Theta1 = Application.WorksheetFunction.Pi() / 2
ElseIf PTheta1 <= -1 Then
Theta1 = -1 * (Application.WorksheetFunction.Pi() / 2)
End If
If Abs(PTheta2) < 1 Then
Theta2 = Atn(PTheta2 / Sqr(Abs(1 - PTheta2 ^ 2)))
ElseIf PTheta2 >= 1 Then
Theta2 = Application.WorksheetFunction.Pi() / 2
ElseIf PTheta2 <= -1 Then
Theta2 = -1 * (Application.WorksheetFunction.Pi() / 2)
End If
If Max < Base Then
Sinhalf = 0
ElseIf Max > Ceiling And Min > Ceiling Then
Sinhalf = 0.5 * (Ceiling - Base)
ElseIf Max < Ceiling And Min > Base Then
Sinhalf = 0.5 * (Taverage - Base)
ElseIf Max < Ceiling And Min <= Base Then
Sinhalf = ((1 / (2 * WorksheetFunction.Pi()))) * ((W * Cos(Theta1)) + ((Taverage - Base) * ((WorksheetFunction.Pi() / 2) - Theta1)))
ElseIf Max >= Ceiling And Min > Base And Min < Ceiling Then
Sinhalf = (1 / (2 * WorksheetFunction.Pi())) * ((Taverage - Base) * (Theta2 + (WorksheetFunction.Pi() / 2)) + (Ceiling - Base) * ((Application.WorksheetFunction.Pi() / 2) - Theta2) - W * Cos(Theta2))
ElseIf Max >= Ceiling And Min <= Base Then
Sinhalf = (1 / (2 * WorksheetFunction.Pi())) * ((Taverage - Base) * (Theta2 - Theta1) + W * (Cos(Theta1) - Cos(Theta2)) + (Ceiling - Base) * ((WorksheetFunction.Pi() / 2) - Theta2))
End If
End If
SinDD = SinDD + Sinhalf
Next n
End Function
My issue is that this calculation is being repeated about 40,000 times, and seems a bit slow on some older computers I use it on. I'm wondering if the ElseIf statement can drag down calculation times and if another If or Select statement would be more efficient?
Last edited: