performance - 嵌套for循环运行时间计算

标签 performance big-o

如何计算该循环最坏情况的运行时间?

for(int i=1 ; i * i < n ; i*=2)

{
     //do something
}

最佳答案

让我们一起努力——

第一:

for (int i = 1; i < n; i *= 2) --> log(n)

然后:

for (int i = 1; i * i < n; i ++) --> sqrt(n)

所以,看起来你的循环会受到 log(sqrt(n)) 的影响

关于performance - 嵌套for循环运行时间计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13002478/

相关文章:

c - 你为什么用汇编编程?

c - 如何计算算法的运行时间?

algorithm - 概率空间和预期运行时间 : what are they?

algorithm - 如何根据以下代码计算 Big-O Notation

php - memcached满了怎么替换数据,memcache性能如何?

c++ - 生成类唯一标识的有效方法?

python - 如何在 Python + Pygame 中提高帧率 (fps)?

mysql - mysql 中的行解释比实际行大

arrays - 查找具有给定总和的已排序数组的子集的最有效算法

algorithm - O、Ω 和 Θ 之间有什么区别?