Friday 4 December 2009

Basic Performance Tricks

These (language agnostic) performance tricks are rather basic, but in the current programming world where most of the thinking revolves around adding more layers or more objects (one more interface for decoupling, one more class for better cohesion)... these things involving binary operators seem almost like assembler for many of us (at least for me) but it should not be that way... Performance tricks like these can help to balance the performance downsides inherent to modern (and necessary) techniques like virtual calls, delegate calls (in .Net world), dynamic languages... and anyway, it's fun stuff to know about :-)

So, there we go (to understand it just think it in binary with a couple of samples):


  • Check if a number is odd or even:
    this is faster than using the classic module operation.
    if (num & 1 == 1)
    // num is odd,
    else
    // num is even

  • Check if a number is a power of 2 ( I found this among the comments of this interesting codeproject article)

    //for num >= 1
    (num & (num-1)) == 0


  • and well, this is terribly basic, but at least it's a good sample to me of what bit shift operations can be useful for (outside low level programming):
    Multiply for/divide by a multiple of 2:

    num << 1 == num * 2
    num << 2 == num *4
    ...
    num >> 1 == num / 2
    num >> 2 == num / 4



As an additional note, checking the Bitwise operation entry in wikipedia, I found this:
On most older microprocessors, bitwise operations are slightly faster than addition and subtraction operations and usually significantly faster than multiplication and division operations. On modern architectures, this is not the case[1]: binary operations are generally the same speed as addition (though still faster than multiplication).

And well, this is not language agnostic, but C# and VB.Net (by the way VB, you've ever been ugly and will ever be, regardless of how many cosmetics MS applies on you) specific, but given that we're talking about performance and it's so short that does not deserve a full blog entry:
Cost of Method Calls:

  • Virtual method call time is 1x.

  • Delegate method call time is 1.5x

  • Interface method call time is 2x


No, I've not done any investigation myself further than finding the info here. Delegate calls being more expensive than Interface calls caught me a bit by surprise, thought well I'd never done serious thinking about it.

2 comments:

  1. Wow, these ol' skool tricks get me back 15 years in time, when I started coding my own graphics library: I learned that the usual 320x200 screen was just an array of 64000 bytes at memory 0xA000 - if you wanted to draw a pixel in that screen, on location (x,y), you would set the byte at memory [0xA000 + 320*y + x].
    But a much much quicker way (although not the quickest) was via bit shifting: [0xA000 + y<<8 + y<<6 + x], which is the same as splitting the first formula: 0xA000 + 256*y + 64*y + x.
    You've brought me today fond memories about long nights of 3D coding in Assembly and C, before Windows added a layer of "coding bureaucracy" that killed my joy of coding.

    ReplyDelete
  2. hahaha, this has much to do with what we were discussing yesterday when going back home :-)
    I many times regret of having missed the Assembly coding days... but then I think that if I had enjoyed the power of almost "touching" the hardware with my code would make me hate current programming trends...

    ReplyDelete