且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

时间复杂度和运行时间之间的差异

更新时间:2023-09-16 22:34:58

运行时间是程序运行所需的时间。时间复杂度是对运行时间随输入大小趋于无穷大的渐近行为的描述。

Running time is how long it takes a program to run. Time complexity is a description of the asymptotic behavior of running time as input size tends to infinity.

您可以说运行时间是 O(n ^ 2)之类的,因为这是描述复杂性类和big-O表示法的惯用方式。实际上,运行时间不是复杂度类,它是持续时间,或者是为您提供持续时间的函数。 成为O(n ^ 2)是该函数的数学特性,而不是其完整特征。确切的运行时间可能是2036 * n ^ 2 + 17453 * n + 18464个CPU周期,或其他任何时间。并不是您经常需要非常详细地了解它,无论如何,它很可能取决于实际输入以及输入的大小。

You can say that the running time "is" O(n^2) or whatever, because that's the idiomatic way to describe complexity classes and big-O notation. In fact the running time is not a complexity class, it's either a duration, or a function which gives you the duration. "Being O(n^2)" is a mathematical property of that function, not a full characterisation of it. The exact running time might be 2036*n^2 + 17453*n + 18464 CPU cycles, or whatever. Not that you very often need to know it in that much detail, and anyway it might well depend on the actual input as well as the size of the input.