Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to optimize division by 100?

I have a piece of code running into an loop, with a division by 100, which is reducing a little my fps count.

In majority of cases, is a int/uint type being divided by 100, resulting in a simple Number.

I just want to know any way to optimize that.

EDIT: Little benchmark with the @scriptocalypse suggestion - multiplying for 0.01:

import flash.utils.getTimer;

for(var k:Number = 20; k > 0; k--)
{
    var a:int = getTimer();

    var o:Number = 100;
    var p:Number;
    for(var i:Number = 100000000; i > 0; i--)
    {
        p = o * 0.01;  // took 423~510   <--------------
        //p = o / 100;  // took 713~768   <--------------
    }

    var b:int = getTimer();

    trace( b - a);
}
like image 453
Marcelo Assis Avatar asked Dec 05 '25 18:12

Marcelo Assis


2 Answers

I suspect that it's not the division that's causing the bulk of your issues, as even slow math operations should be relatively fast compared to other operations.

While this:

x * 0.01;

should theoretically be faster than

x / 100;

I still suspect that it won't make much difference. What else are you doing in your loop?

like image 164
scriptocalypse Avatar answered Dec 08 '25 15:12

scriptocalypse


Almost certainly, the right answer is don't. Programmers should write code that is clear to read, compilers should optimize.

like image 25
hcarver Avatar answered Dec 08 '25 15:12

hcarver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!