In my previous posts, I presented optimizations for e bunch of Maths functions, so lets continue and see whats next in the list ! Today I am going to explore ways to optimize Math.pow(2,x) or 2^x , as well as Math.exp One easy trick to fast perform 2^i for integers is to use bit shifting, […]

~ read more ~## Posts in category bit twiddling

# Euclidean division

How to fast perform the euclidean division of two integers and compute the rest the of the division at the same time ? The naive approach is to simply get the result using the / and the % operator: q = i/j; r = i%j; As we know, modulo is very slow, so instead, we […]

~ read more ~# min max float in byteArray

In a previous post, I explored ways to compute the Math.min or Math.max for Numbers (http://guihaire.com/code/?p=550) What if we need to perform this operation when the floats are in a ByteArray ? Lets explore some interesting methods So our ByteArray contains a list of floats or numbers, the easiest way is to use the ternary […]

~ read more ~# Math.min Math.max integer tricks

Lets explore options today to fast get the min or the max of 2 integers values. Let’s start with the min, so we want to optimize: min = Math.min(i,j); One easy optimization is to use the ?: ternary operator to avoid the glue function call: min = (i<j)?i:j; we can replace the i<j by (i-j)>>31 […]

~ read more ~# AS3 loop speed

What is the fastest way to loop ? We have an integer i, and we want to increment i until it reach REPS, fastest way ? 1) for(i= 0;i<REPS;i++) 2) for(i=-1;++i<REPS;) 3) for(i= REPS;–i;) 4) for(i= 0;(i-REPS)>>31;i++) 5) for(i= -1;(++i-REPS)>>31;) 6) for(i= -1;(++i-REPS)>>>31;) All those methods are equivalents, except for 3 where we decrease i […]

~ read more ~
## Recent Comments