简体   繁体   English

如何估算使用递归Java的程序的时间和内存消耗

[英]How to estimate the time and memory consumption of a program that uses recursion Java

I'm solving a Knight's tour problem. 我正在解决骑士的旅行问题。 The size of a desk is 5X5, and starting point for a tour can be any square. 桌子的大小是5X5,游览的起点可以是任何正方形。 I found all possible open solutions, and calculated the memory usage as well as time consumption of a program. 我找到了所有可能的开放式解决方案,并计算了内存使用率以及程序的时间消耗。 I'm using recursion and on each knight' move I'm calculating the next possible moves for a knight. 我正在使用递归,并且在每个骑士的动作上,我正在计算一个骑士的下一个可能动作。

The questions is how to calculate memory and time consumption on a much larger desk. 问题是如何在更大的桌面上计算内存和时间消耗。 What kind of tools are usually used in Java to estimate these values for programs that are impossible to actually run? Java中通常使用哪种工具来估算无法实际运行的程序的这些值? Should it be just assumption using O-notation? 应该只是使用O标记的假设吗?

There are no Java Tools or Tools in other programming languages to do this in General. 通常,没有Java工具或其他编程语言的工具可以做到这一点。 It is related to the Turing halting problem , which is known to be unsolveable in common. 它与Turing暂停问题有关 ,众所周知这是无法解决的。

For your concrete Problem instance you could write one, that tries to extrapolate from your measurments with smaller size boards using a theoretical analysis of the concrete algorithm (O-Notation)). 对于具体的问题实例,您可以编写一个实例,尝试使用具体算法的理论分析(O表示法)从较小尺寸的电路板中得出的测量值进行推断。

Eg if you know, that runtime is O(2^n), which means 例如,如果您知道运行时间为O(2 ^ n),则意味着

t = c * (2 ^ n) ( + neglectable parts) t = c *(2 ^ n)(+可以忽略的部分)

you can compute the constant c by setting in the equation the concrete time for eg n=5, eg if you measure t=10s for n=5: 您可以通过在方程式中设置具体时间(例如n = 5)来计算常数c,例如,如果在n = 5时测量t = 10s:

10s = c * (2 ^ 5) 10秒= c *(2 ^ 5)

==> c = 10 s / (2 ^ 5) ==> c = 10 s /(2 ^ 5)

This is only an example (I dont know if your Problem is O(2 ^n)). 这只是一个例子(我不知道您的问题是否为O(2 ^ n))。

But as I said, for this you have to know the O-Notation of the algorithm, which comes from proofs found by human mathematical intuition, and which is not computable in General by a god-algorithm. 但是正如我说的,为此,您必须知道算法的O表示法,它是由人类数学直觉得出的证明而来的,通常不能通过神算法来计算。

What is the scale-up to your much larger desk ? 扩大到更大的办公桌的尺寸是多少

Please note that there can be a significant difference between calculated consumption (of both time and memory) and the consumption estimated from the complexity. 请注意, 可以计算出消耗(的时间和内存),并从复杂性估计消耗之间的差异显著。 The previous answer (Thomas Philipp) is correct except for one detail: 前面的答案(Thomas Philipp)是正确的,但有一个细节:

t = c * (2 ^ n) ( + neglectable parts) t = c *(2 ^ n)(+可以忽略的部分)

From one theoretical standpoint, this is a contradiction: if you care about the factor c , you also may care about the so-called "neglectable parts". 从一个理论的角度来看,这是一个矛盾:如果您关心因子c ,那么您可能还会关心所谓的“可忽略的部分”。 Those drop out in a complexity determination, the O(2^N) world, where the only term that counts is the one that dominates the limit at +infinity. 那些在复杂度确定的O(2 ^ N)世界中消失了,其中唯一重要的项是在+ infinity处限制极限的项。

In practical terms, check your set-up complexity and any looming secondary terms in your algorithm. 实际上,请检查您的设置复杂性以及算法中任何迫在眉睫的次要条件。 For instance, one program I worked on had a straightforward O(n^2 log n) solution. 例如,我开发的一个程序具有简单的O(n ^ 2 log n)解决方案。 There was O(n log n) pre-work and some O(n) overhead. O(n log n)个准备工作和一些O(n)开销。

The problem we faced was that, to our consumers, the algorithm didn't appear to scale that way. 我们面临的问题是,给我们的消费者,该算法并没有出现按比例的方式。 For a small task, the overhead dominated. 对于小任务,开销占主导地位。 For a typical evaluation task, the pre-work and main body were of roughly equal time. 对于典型的评估任务,前期工作和主体的时间大致相等。 For a true application, the main body showed its true colours and took over, although the first two stages then took longer than an eval task's entire run. 对于真实的应用程序,主体显示其真实的颜色并接管了工作,尽管前两个阶段花费的时间比评估任务的整个运行时间长。

In short, the medium-term computations did not scale as an external viewer would expect, because of the high constant and coefficient values in the lower-complexity stages. 简而言之,由于较低复杂度阶段的常数和系数值较高,因此中期计算无法按外部观察者的预期进行扩展。

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

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