开发者

Fastest way to make Negative numbers

开发者 https://www.devze.com 2023-03-12 01:50 出处:网络
Is there any Fastest way for this line? ballAngelRadianVector = -ballAngelRadianVector; and also 开发者_高级运维this:

Is there any Fastest way for this line?

ballAngelRadianVector = -ballAngelRadianVector;

and also 开发者_高级运维this:

ballDegree = fee - ballDegree ;


I don't think you can get faster than that.. See this quick check I did:

var i:uint = 0;
for(i; i<1000000; i++)
{
    var a:int = -i;
}

trace(getTimer()); //14


i was actually wondering something similar strictly from curiosity (i know this is not the bottleneck of my application). my question is, is it easier to set a var to a negative of itself, or to multiply it by -1. i am wondering if this varies from CPU, OS, etc, but i ran the following test:

$number = rand(100000,999999999);
$iterations = 10000000;

$start = microtime(true);

for($i = 0; $i <= $iterations; $i++)
    $number = -$number;

echo "time: ".(microtime(true)-$start)."\n";
//
$start = microtime(true);

for($i = 0; $i <= $iterations; $i++)
    $number = $number * -1;

echo "time: ".(microtime(true)-$start)."\n";
//
$start = microtime(true);

for($i = 0; $i <= $iterations; $i++)
    $number = -$number;

echo "time: ".(microtime(true)-$start)."\n";
//
$start = microtime(true);

for($i = 0; $i <= $iterations; $i++)
    $number = $number * -1;

echo "time: ".(microtime(true)-$start)."\n";

which produced the output:

time: 0.66124606132507 (-self)
time: 0.64714503288269 (*-1)
time: 0.66628909111023 (-self)
time: 0.65639805793762 (*-1)

so it looks like multiplying by -1 is consistently faster (by a negligible amount)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号