More Efficient Code

Blitz3D Forums/Blitz3D Beginners Area/More Efficient Code

Mental Image(Posted 2004) [#1]
Can someone make this more efficient for me, please (it shouldn't be too difficult!).

For x=warning_pos To warning_pos+(warning_max-2)
y1=warning_store(x)
If y1>100 Then y1=100
y2=warning_store(x+1)
If y2>100 Then y2=100
Line xpos,20+(300-(3*y1)),xpos+20,20+(300-(3*y2))
xpos=xpos+20
Next

OK, "warning_pos" is just a pointer into an array, and "warning_max" is the limit of iterations. "Warning_Store" is an array that will usually contain values between 0 and 100, but MAY contain values higher than that. I am plotting the values on an axis which ranges from 100 at the top to 0 at the bottom. The top of this axis is located at screen position y=20 (hence the 20+.....) and each vertical position is separated by 3 pixels (hence 3*y). I want to clip any value greater then 100. This is drawing a line graph between succesive points, by the way.

It works fine, but is messy. Show me how to be elegant!


jhocking(Posted 2004) [#2]
It mostly looks good to me. I mean, I would format it a little differently but the content would be the same. Well, you could change 20+(300-(3*y1)) to just 320-3*y1. Frankly though, depending on the context (ie. the rest of your code) that may make things confusing. Definitely don't leave 20 hanging out like that, but it might make sense to set a constant to 20 at the top of your code and use that constant here, as in:

Const offset=20
...
Line xpos,offset+300-3*y1...

Incidentally, efficient and elegant are often two very different things. The most efficient code is often quite inelegant, and thus, unless you really need to optimize the routine, undesirable because it is harder to understand. For example, replacing all the magic numbers in your code with named constants would be more elegant but less efficient.


Shambler(Posted 2004) [#3]
A tough one, there's not much to optimise.

Depending on how often you are updating the values in the array 'warning_store()' it may be better to clip the values at 100 when you store them in the array rather than every time you read from the array.

If the updating of the values is very rarely you might benefit from multiplying by 3 and adding 20 when you store them rather than when you read them, also storing them 300- may speed things up and simplify and speed up this loop.

In short prepare the values to be correct for reading when you write them.

Can't think of anything else atm.


eBusiness(Posted 2004) [#4]
AnExtraVar=warning_pos+warning_max-1
y1=warning_store(warning_pos) 
If y1>100 Then y1=100
For x=warning_pos+1 To AnExtraVar
y2=warning_store(x) 
If y2>100 Then y2=100 
Line xpos,320-3*y1,xpos+20,320-3*y2
xpos=xpos+20 
y1=y2
Next

I think this is the best I can do, should be faster.


jhocking(Posted 2004) [#5]
Good one. I didn't think of reducing the number of calls into the array by storing the value of y2 in y1 every time through the loop. Odd that I didn't think of that since I do it all the time, but whatever.


Mental Image(Posted 2004) [#6]
Cool - thanks guys.

I can't clip when storing the values in the array, because I use the genuine values elsewhere. I could duplicatet he arrays, but am not overly bothered about that much of a speed hit, just wanted to know if it could be done better.

Excellent point about the relative merits of elegant vs efficient Joe.

Thanks again,

Paul


Oso(Posted 2004) [#7]
Yes indeed, Joe, very interesting. I had to teach myself programming and I found the only sure way I could code a complicated algorithm and know it would work was to realise that at bottom all instruction can be thought of as a collection of two-way decisions with labels before them. In other words either keep going or jump somewhere else. I used this way to programme professionally for years. My code looked like a dog's breakfast to most people and my flowcharts sprawling collections of little inverted "Y"s, but it always ran very quickly and had remarkably few bugs.

I understand structure now, (I have to for modifications to other people's code at work) but here at home, for really complicated recursive things of my own (geometrical puzzle solvers or mathematical work) - I still run riot and fall back on what is essentially a collection of forks with gotos and labels because I know I can always make it work.

So I have a foot in each camp, but I'm afraid at heart I am much closer to efficiency than to elegance.