简体   繁体   English

20 * 20 亿在 Big O 表示法中如何与 2 * 3 一样长?

[英]How does 20 * 2 billion take as long as 2 * 3 in Big O Notation?

Im studying big O and I came across this我正在学习大 O,我遇到了这个

function timesTwo(num) {
  return 2 * num
}
let result = timesTwo(5) // 10
let result2 = timesTwo(2000) // 4000

then it says this然后它说这个

Now, which of these do you think will take the longest to compute?现在,您认为其中哪些将花费最长的时间来计算? 2 * 5 or 2 * 2000?...It's just one operation (one multiplication). 2 * 5 还是 2 * 2000?...这只是一次运算(一次乘法)。 20 * 2 billion takes as long as 2 * 3. No matter the size of the input, the function takes the same amount of time to compute. 20 * 20 亿与 2 * 3 一样长。无论输入的大小如何,该函数都需要相同的计算时间。

How is that true??怎么是真的?? how does that work?这是如何运作的? it seems to me that 20 * 2 billion would take significantly longer the 2 * 3在我看来,20 * 20 亿需要比 2 * 3 更长的时间

To make matters more unclear, it goes on to say this.为了让事情更清楚,它继续说这个。

function manyTimes(num) {
  let total = 4 * num
  return total * 3
}

Now, we wouldn't say this function has a Big O of 2, it'd still just be a Big O of 1 because we're looking at the big picture (1 operation isn't gonna take significantly longer than 2 for a computer so we can just ignore it) .现在,我们不会说这个函数的 Big O 为 2,它仍然只是一个 Big O 为 1,因为我们着眼于全局(1 次操作不会比 2 花费更长的时间)电脑,所以我们可以忽略它) No matter what we put in, the number of operations won't increase in the function, it's constant time.无论我们放入什么,函数中的操作数都不会增加,它是恒定的时间。

can you please explain the bolded text above.. ok 1 operation isnt significantly longer than 2. But what about 1 compared to 20,000 operations?你能解释一下上面的粗体文本吗?好吧 1 次操作并不比 2 长得多。但是 1 与 20,000 次操作相比又如何呢?

Source where I read this information.我阅读此信息的来源。 Big O Notation In Javascript Javascript 中的大 O 表示法

Big O doesn't refer to exactly how long it takes for something to process, but rather how the length of the process grows with N inputs.大 O 并不是指处理某件事需要多长时间,而是指处理的长度如何随着 N 个输入而增长。 Linear functions grow more slowly than exponential functions, meaning that Linear is faster to process in Big O.线性函数比指数函数增长得更慢,这意味着线性函数在 Big O 中的处理速度更快。

Your example here compares two linear expressions, meaning that the growth for both is the same.您在此处的示例比较了两个线性表达式,这意味着两者的增长是相同的。 I would say that it is inaccurate to say that they take the same time however, but rather that they grow at the same rate.我想说的是,说它们花费相同的时间是不准确的,而是说它们以相同的速度增长。

it's about tail behavior with input n stretching upwards to infinity.这是关于输入 n 向上延伸到无穷大的尾部行为。 we are only concerned about how the tail of computational complexity trends at the end stages of that tail, because we would generally want to build algorithms in mind for the worst case scenario.我们只关心计算复杂度的尾部在该尾部的最后阶段如何趋势,因为我们通常希望在最坏的情况下构建算法。 20x2billion is still O(n) just as 2x3 is still 0(n) 20x20 亿仍然是 O(n),就像 2x3 仍然是 0(n)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM