本文介绍了为什么需要更长的时间才能循环100亿比10亿?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这个循环(到10亿)只需要几个声音执行...

  

我想 i 是一个32位的整数变量,因此总是小于100亿(大于2 ^ 32),而10亿仍然适合32位的范围(其结束于大约2或4十亿,取决于签名)。虽然我不知道编译器如何提升这个100亿恒定值,但他似乎意识到溢出问题,并使其成为一个无用的循环。



当你使我 a long long int (也许 10000000000 a 10000000000L ,但这似乎没有问题)?


Why does this loop (to 1 billion) only take a few sounds to execute ...

for (i = 0; i < 1000000000; i++)
{

}

... but this loop (to 10 billion) takes >10 minutes?

for (i = 0; i < 10000000000; i++)
{

}

Shouldn't it just take 30 seconds or so (3 seconds x 10)?

解决方案

I guess i is a 32-bit integer variable and is therefore always smaller than 10 billion (which is more than 2^32), whereas 1 billion still fits into the 32-bit range (which ends at about 2 or 4 billion, depending on signedness). Though I don't know how the compiler promotes this 10 billion constant, but he seems to realize the overflow issue and makes it an infite loop.

What happens when you make i a long long int (and maybe the 10000000000 a 10000000000L, but that seems to be no problem)?

这篇关于为什么需要更长的时间才能循环100亿比10亿?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 16:40