运行时间和内存
如果您看不到函数的代码,但知道它需要参数。是否可以找到运行时间速度和内存。如果是这样你会怎么做。在这种情况下有办法使用 Big O 吗?
If you cannot see the code of a function, but know that it takes arguments. Is it possible to find the running time speed and memory. If so how would you do it. Is there a way to use Big O in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不,仅通过查看函数的参数是不可能找到函数的内存或性能的。例如,相同的函数
可以实现为 O(1) 时间和内存:
或者实现为一个非常非常昂贵的函数,需要 O(x*y*z):
以及许多其他可能性。因此,无法得知该功能的成本有多高。
No, it's not possible to find either the memory or performance of a function by just looking at its parameters. For example, the same function
Can be implemented as O(1) time and memory:
or as a very, very expensive function taking O(x*y*z):
And many other possibilities. So, it's not possible to find how expensive the function is.
我可以运行该函数吗?多次?
我将使用一系列参数值执行该函数,并测量每次运行的运行时间和(如果可能)内存消耗。然后,假设该函数采用 n 参数,我会将每个数据点绘制在 n+1 维图上,并从那里寻找趋势。
Am I allowed to run the function at all? Multiple times?
I would execute the function with a range of parameter values and measure the running time and (if possible) the memory consumption for each run. Then, assuming the function takes
n
argument, I would plot each data point on ann+1
-dimensional plot and look for trends from there.首先,这是一个面试问题,所以你最好不要说不。
如果我参加面试,这就是我的方法。
我可能会问面试官一些问题,因为面试是互动的。
因为我看不到代码,所以我想我至少可以多次运行它。这是我的第一个问题:我可以运行它吗? (如果我无法运行它,那么我实际上什么也做不了,然后我就放弃了。)
该函数的用途是什么?如果函数编写得合理的话,这可能会提示复杂性。
论证的类型有哪些?有些是原始类型吗?尝试它们的一些组合。其中一些是否“复杂”(例如容器)?尝试一些不同尺寸的组合。其中一些是否相关(例如,一个与容器相关,另一个与容器的大小相关)?可以保存一些测试运行。另外,我希望给出论证的合法范围,这样我就不会浪费时间在非法猜测上。最后,测试一些边缘案例可能会有所帮助。
First of all, it is an interview question, so you'd better never say no.
If I were in the interview, here is my approach.
I may ask the interviewer a few questions, as an interview is meant to be interactive.
Because I cannot see the code, I suppose I can at least run it, hopefully, multiple times. This would be my first question: can I run it? (If I cannot run it, then I can do literally nothing with it, and I give up.)
What is the function used for? This may give a hint of the complexity, if the function is written sanely.
What are the type of argument? Are some they primitive types? Try some combinations of them. Are some of them "complex" (e.g. containers)? Try some different size combinations. Are some of them related (e.g. one for a container, and one for the size of the container)? Some test runs can be saved. Besides, I hope the legal ranges of the arguments are given, so I won't waste time on illegal guesses. Last, to test some marginal cases may help.
你能用代码运行该函数吗?像这样的东西:
Can you run the function with a code? something like this:
作为一个面试问题,你永远不应该回答“不,这是不可能的”。
您需要的是运行代码的能力。一旦可以运行代码,使用不同的参数调用相同的函数并测量所需的内存和时间。然后您可以绘制这些数据并获得良好的估计。
对于大 O 类型符号,您也可以遵循相同的方法并根据数据集大小绘制结果。然后尝试使用最小二乘拟合将该曲线与已知的复杂度曲线(如 n、n^2、n^3、n*log(n)、(n^2)*log(n) 等)拟合。
最后,请记住所有这些方法都只是近似值。
Being an interview question, you should never answer like "no it cannot be done".
What you need is the ability to run the code. Once you can run the code, call the same function with different parameters and measure the memory and time required. You can then plot these data and get a good estimate.
For big-O type notations also, you can follow the same approach and plot the results WRT the data set size. Then try to fit this curve with the known complexity curves like n, n^2, n^3, n*log(n), (n^2)*log(n) etc using a least square fit.
Lastly, remember that all these methods are approximations only.
不,你不能,这将解决 停止问题 ,因为代码可能会无限运行 O(infinity) 。因此,解决这个问题也就解决了惠普,这当然被证明是不可能的。
no you cannot, this would have solved the Halting Problem , since code might run endlessly O(infinity). thus, solving this problem also solves HP, which is of course proven to be impossible.