[英]Redis time complexity of O(log(N)+M)
Time complexity :
O(log(N)+M)
withN
being the number of elements in the sorted set andM
the number of elements returned. 时间复杂度 :O(log(N)+M)
,N
为排序集中的元素数,M
为返回的元素数。
This is from Redis docs. 这是来自Redis文档。 I understand the concept of Big O but I have my doubts about what role does log()
play in this context. 我了解Big O的概念,但是我对log()
在这种情况下扮演什么角色感到怀疑。 I read about log
and answers in SO, eg it can have different bases. 我读过SO中的log
和答案, 例如它可以有不同的依据。
Could someone explain this time complexity with few examples? 有人能用几个例子解释这种时间的复杂性吗?
For example: N = 1000, M = 10
and N = 1,000,000, M = 1000
例如: N = 1000, M = 10
和N = 1,000,000, M = 1000
Remember that big-O notation talks about long-term growth rates, rather than the actual numerical number of steps required by some algorithm. 请记住,big O表示的是长期增长率,而不是某些算法所需的实际数字步数。 If a function's runtime is 100log N + 200M or 3log N + 150M, in both cases the runtime is O(log N + M), so just knowing the big-O notation won't let you predict the runtime on a given N and M a priori. 如果函数的运行时为100log N + 200M或3log N + 150M,则在两种情况下,运行时均为O(log N + M),因此仅知道big-O表示法就无法预测给定N和先验
What you can do is use your knowledge of the fact that the runtime is O(log N + M) to extrapolate the runtime given some data points. 您可以做的是利用已知的运行时为O(log N + M)的事实来推断给定一些数据点的运行时。 For example, if you know the runtime when N = 10,000,000 and M = 1,000 is 1s, you can predict that the runtime when N = 10,000,000 and M = 2,000 will probably be something like 2s because the runtime scales linearly as a function of M. The more data points you have to work with, the better your prediction will be. 例如,如果您知道N = 10,000,000和M = 1,000时的运行时间为1s,则可以预测N = 10,000,000和M = 2,000时的运行时间可能约为2s,因为运行时间随M线性变化。您必须使用的数据点越多,您的预测就越好。 You can also see that the runtime is far less sensitive to changes in N than in M, since for log N to increase by a factor of two you'd need to effectively square the value of N. 您还可以看到,运行时对N的变化的敏感性远不如对M的敏感,因为要将log N增加2倍,您需要有效地平方N的值。
Let me dissect this thing part by part... 让我逐一剖析这件事...
O(log n): logarithmic complexity O(log n):对数复杂度
To understand this complexity class you first need to develop an intuition around the log
function. 要理解此复杂性类,您首先需要围绕log
函数开发一个直觉。
You have: 你有:
ln
: natural logarithm, or logarithm base e
ln
:自然对数,或以e
底的对数 log
: binary logarithm, or logarithm base 2. Unless another base is specified log
:二进制对数,或以2为底的对数。 log*
: also called the iterated logarithm log*
:也称为迭代对数 Complexity classes frequently use the binary logarithm, or logarithm base 2 rather than the rest. 复杂度类经常使用二进制对数或以2为底的对数而不是其余数。 But it's still important to make a distinction between those . 但是,区分两者仍然很重要 。
The log
function is defined by "the power to which the number 2 must be raised to obtain the value n" . 对log
函数由“必须将数字2提高到以获得值n的幂”定义。 However that definition can be a bit hard to reason about for people like you and me. 但是,对于像您和我这样的人,很难定义这个定义。
You can rather understand it in a more informal / less correct way as: "the number of times you can divide by number by 2" . 您可以更非正式地/不太正确地理解它,例如: “可以用数字除以2的次数” 。
8 / 2 = 4
-> 4 / 2 = 2
-> 2 / 2 = 1
. 8 / 2 = 4
> 4 / 2 = 2
> 2 / 2 = 1
。 log(8) = 3
因此log(8) = 3
Wrapping things up: O(log n)
means that if the input has size n
, the time used by the function will be proportional to log n
. 整理一下: O(log n)
表示如果输入的大小为n
,则函数使用的时间将与log n
成正比。
O(log n)
is much faster than O(n)
. O(log n)
比O(n)
快得多。
O(n): Linear complexity O(n):线性复杂度
This means that if the input has size n
, the time used by the function will be proportional to n
. 这意味着,如果输入的大小为n
,则函数使用的时间将与n
成正比。 Meaning, it has a 1:1 proportionality. 意思是,它具有1:1的比例。
Different variables 不同的变量
Now, what to do when you have different variable names... 现在,当您使用不同的变量名时该怎么办...
In this case, N and M speak of different variables. 在这种情况下,N和M代表不同的变量。 Some examples 一些例子
M x N
(eg: M rows and N columns) matrix can take O(M * N)
time. 遍历M x N
(例如:M行N列)矩阵可能需要O(M * N)
时间。 V
number of vertices and E
number of edges can have its complexity defined as a function of V
and E
, eg: O(V + E)
or O(V * E)
or whatever. 对具有V
个顶点和E
个边的图执行运算的算法可以将其复杂度定义为V
和E
的函数,例如: O(V + E)
或O(V * E)
或其他。 Finally, going back to your question: 最后,回到您的问题:
What does O(log(N) + M)
mean in this particular context? 在这种特定情况下, O(log(N) + M)
是什么意思?
Means that it will be the sum of: 表示它将是以下各项的总和:
O(log N)
in terms of the elements of the set 根据集合的元素为O(log N)
O(N)
in terms of the elements being returned (the query results) 根据要返回的元素(查询结果)为O(N)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.