Saturday, 6 June 2015

Snootiness & Program Optimisation

For Programmers

I was a member of stackOverflow for a while. It's a great resource appearing in many Google search results for my programming queries.

I stopped posting questions due to the pedants and insufferable bores who preferred to mock a question rather than keep quiet because they didn't know the answer.

In my book: there are no stupid questions and mocking a person's lack of knowledge devalues their own knowledge.

Recently I heard this (and I paraphrase and name-change)

Adam: what is ASCII 65?
Eve: You mean you don't know? (Turning to a colleague and rolling her eyes) He doesn't know what ASCII 65 is?! Ha! What a numpty.

Eve's mocking of Adam for not knowing ASCII 65 is an upper case 'A' devalued her knowledge of it. It lowered the value of her knowledge to the level of 'everyone on the planet should know that not just me with my IT qualifications'.
Silly Eve.

Anyway, while traversing stackOverflow I came across this as a solution:

byte Patch(byte delta, byte lo)
{
    int resultPixel = (2 * (delta - 127)) + lo;

    if (resultPixel > 255)
       resultPixel = 255;

    if (resultPixel < 0)
       resultPixel = 0;

    return (byte)resultPixel;
}

Missing out an 'else' will make force the system to perform the second test even though the first test is true. As this Patch() was to be called many thousands of times, this simple over-site will result in a considerable loss in performance.
It should have read:

byte Patch(byte delta, byte lo)
{
    int resultPixel = (2 * (delta - 127)) + lo;

    if (resultPixel > 255)
       resultPixel = 255;
   else if (resultPixel < 0)
       resultPixel = 0;

    return (byte)resultPixel;


Showing that there is a time and place for pedanticismistophiles.

No comments:

Post a Comment