Jump to content

Learning from forums


ellestones
 Share

You are about to reply to a thread that has been inactive for 2116 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

on forums when we make suggestions about optimising posted scripts it can sometimes be felt by the poster as a criticism.  When thats not the intent. The intent is for us to learn something that we might not have known, or we do know but someone else reading may not

so I would like to show how code we post ourselves can be critiqued and further optimised. I use this line (which I posted elsewhere previously) as an example:

p.y = (float)((integer)p.y / 4 * 4);

division is typically CPU/MPU-specific. Mono CIL (which Mono LSL compiles down to) typically deals with division by calling a host library function so that the CIL code is portable between hosts    

when we look at this code we can see that the divisor 4 falls on a bit boundary. Knowing this we can rewrite the code to avoid the host library function call altogether and get the same result using a bitwise operation

p.y = (float)((integer)p.y & 0xFFFFFFFC);

integer 3 / 4 * 4 = 0
integer 4 / 4 * 4 = 1
integer 5 / 4 * 4 = 1

hex 0x3 & 0xC = 0
hex 0x4 & 0xC = 1
hex 0x5 & 0xC = 1

another thing we can do when looking at division for other values is to think about it as a multiplication. Multiplication tends to execute faster. Example:

20.44 / 5 = 4.088

20.44 * 0.2 = 4.088

and so on

which leads me into my point. Self-critique and critique of our code by others helps to make us better programmers  

what I know about this forum is that it is one of the best scripting forums ever. The tenor of this forum is quite cooperative

what happens often is that a scripter will post a code in answer to a question. Quite often an experienced scripter will post a more verbose code, as the code is written to more readily explain the algorithm. Another person can sometimes come along and post a more optimised version and/or a different way of doing it. The first poster often then clicks the Like or Thanks button and leaves it at that

the first poster is often fully aware that what they posted is more verbose than the optimum but the intent is to better show the algorithm. The second poster who is also experienced is aware of this in most cases, but their intent is to build off / add to what was posted already

often we can interchangeably be the first, second and even subsequent poster. And when we are the first poster then we hit the Like or even say to OP to go with the approach that a subsequent poster is suggesting. As the approach (even when sometimes not optimal) is commensurate with the current level of the OP's understanding, shown by their conversational interaction

this is not to say that discussion cannot be robust. In past lives I have had furious discussions at times on this forum and across the street. However when my code (rather than my words) can be shown to be less optimal or less productive then I am always happy to dump my code and/or approach and go with what has been subsequently shown to me by another person

  • Like 4
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 2116 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...