Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a loop that analyses a series of numbers using a sub routine called isLyingInCone and everything is working OK except the speed.

The app takes about 4 hours (that's expected, it crunches through a lot of data) to run and so shortening processes is worthwhile. I'm trying to do this bit by bit and I'm sure there is much to do elsewhere, but I discovered a main culprit was the following IF statement and I cannot understand why the lines below should DOUBLE the analysis time from about 4 to 8 hours. Any idea please?


VB
Dim XTRange As Double
Dim NearAngle As Double
Dim FarAngle As Double
Dim myAngle As Double

If XTRange < 500 Then myAngle = NearAngle Else myAngle = FarAngle 'using this doubles the time ! A select Case instead made little difference

'myAngle = FarAngle 
'If I uncomment the line above it speeds up again - even when leaving the above IF line intact.

If isLyingInCone(Xx, Xy, Xz, Tx, Ty, Tz, Bx, By, Bz, myAngle) = True Then
'here we do the maths
End if


Any help? Im baffled as I'd have thought the IF statement would run anyhow. The difference in time in just adding the
VB
myAngle = FarAngle
line is enormous at 4 - 8 hrs

Thanks
Posted
Comments
ZurdoDev 16-Oct-15 14:40pm    
Perhaps something like
myAngle = If (XTRange < 500.00, NearAngle, FarAngle)

Note, I did 500.00 instead of 500. I have not tested this but I believe the compiler is converting 500 to a double from an int and that conversion every time may be adding some of your time.
PIEBALDconsult 16-Oct-15 14:51pm    
That's one of my thoughts as well.
SheepRustler 16-Oct-15 14:56pm    
Sorry I thought Id posted a reply but it doesnt seem to have appeared:

Thanks but no joy, I tried :

Dim Threshold as double = 500.0 (VB strips off the second 00 ?)

and using

myAngle = If (XTRange < Theshold, NearAngle, FarAngle)

but no difference Im afraid....
ZurdoDev 16-Oct-15 14:57pm    
And what was the code like before? When it took half the time?
SheepRustler 16-Oct-15 15:00pm    
Weirdly exactly the same but with the refining line under the if statement

myAngle = If (XTRange < Theshold, NearAngle, FarAngle)
myAngle = FarAngle 'adding this halves the run time approx

1 solution

There is no solution with the code you provided.
myAngle is a parameter to what is eating time, so your code have no interest.
You need to run your code on profiler with a small sample of data;
The profiler will tell you where your program is spending time. You will probably see that a small set of lines or a function is eating all the time.
This function is what you need to list with a couple of example parameters, and then it will be possible to understand why it is so long and if it is possible to optimise your code.

This kind of task is usually performed by professional programmer and with full project source code and enough data to see what append.
All is needed because this is an iterative process.
Profiler > see time eater > tweak > profiler > see differences > tweak again ...

The programmer need to fully understand the guilty part of your code in order to optimise it and be really efficient.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900