求负数最快的方法

发布于 2024-11-14 19:42:45 字数 165 浏览 3 评论 0原文

这条线有没有最快的方法?

ballAngelRadianVector = -ballAngelRadianVector;

还有这个:

ballDegree = fee - ballDegree ;

Is there any Fastest way for this line?

ballAngelRadianVector = -ballAngelRadianVector;

and also this:

ballDegree = fee - ballDegree ;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

や三分注定 2024-11-21 19:42:45

我不认为你能比这更快..请参阅我所做的快速检查:

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

trace(getTimer()); //14

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
陪你到最终 2024-11-21 19:42:45

我实际上出于好奇而想知道类似的东西(我知道这不是我的应用程序的瓶颈)。我的问题是,将 var 设置为自身的负数或将其乘以 -1 是否更容易。我想知道这是否因CPU、操作系统等而异,但我运行了以下测试:

$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";

产生了输出:

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

所以看起来乘以-1始终更快(可以忽略不计)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文