在已知 O(n) 和系统时钟的情况下,我们可以计算代码的执行时间吗? [英] With O(n) known and system clock known, can we calculate the execution time of the code

查看:39
本文介绍了在已知 O(n) 和系统时钟的情况下,我们可以计算代码的执行时间吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码的时间复杂度是已知的.执行程序的系统是 Intel Corei3,它是双核和 CPU @ 2.4ghz — 它有 4 个逻辑处理器.有了这些细节,如何计算代码的执行时间?

The time complexity of the code is known. The system on which the program was executed is Intel Corei3 which is dual core and CPU @ 2.4ghz — it has 4 logical processors. With these details, how can the execution time of the code be calculated?

public class PerfmTest {
    public static void main(String[] args) {
      getexeTime(1000000);
    }

    public static void getTime (long n) {
      // long startTime = System.currentTimeMillis();
      long startTime = System.nanoTime();
      long k = 0;
      for (int i = 1; i <=5; i++) {
          // k = k + 5;
      }
      // long endTime = System.currentTimeMillis();
      long estimatedTime = System.nanoTime() - startTime;
      //System.out.println("Execution time for n = " + n + " is " + (endTime - startTime) + " milliseconds");
      System.out.println("Execution time for n = " + n + " is " + estimatedTime + " nanoseconds");
    }
 }

输出为 855 纳秒.

The output was 855 nanoseconds.

推荐答案

如果我理解正确,您是在问我们是否可以通过了解系统规格和相关代码来计算运行时间.答案是不,我们无法计算执行时间.

If I understand you correctly, you are asking if we can calculate running time by knowing system specs and the code in question. The answer to this is no, we cannot calculate the execution time.

原因是代码不是孤立运行的.这四个处理器不运行您的代码.操作系统在后台做事,服务在运行等等.

The reason is that the code is not run in isolation. Those four processors are not only running your code. The operating system is doing things in the background, services are running, and so on.

事实上,我们不仅无法计算代码的运行时间,而且如果我们已经知道了运行时间,我们甚至无法预测未来的运行时间.在第二次运行代码期间可能会发生任何可能改变输出的事情.更多上下文切换、更多后台任务或其他任何内容.

In fact, not only can we not calculate the running time of the code, but we cannot even predict future running times if we already know the running time. Anything might happen during a second run of the code that could change the output. More context switches, more background tasks, or anything else.

在运行性能测试时,我曾多次亲身体验过这些影响.如果计算机闲置时间更长,或者如果上次启动是冷启动而不是待机/恢复等,则相同的测试套件将产生不同的时间.

I have experienced these effects firsthand many times when running performance tests. The same test suite will produce different timings if the computer has been sitting idle for longer, or if the last boot was a cold boot as opposed to a standby / resume, etc.

如果您询问如何衡量代码的运行时间,以上所有内容仍然适用.从本质上讲,您是在谈论进行实验.在实验中,必须控制所有外部变量.系统在每次运行测试时都必须处于相同的状态,这在技术上是可以实现的,但肯定远远超出了本问题的范围.

If you are asking about how to measure the running time of your code, all of the above still apply. You are talking, in essence, about running an experiment. In an experiment, all external variables must be controlled. The system must be in the same state for each run of the test, which is technically possible to achieve, but certainly far beyond the scope of this question.

对于任何合理的成功预期,我们可以做的唯一一件事就是预测算法 A 将比算法 B 执行得更好(即使这样有时也会针对不同的输入、输入大小等产生意想不到的结果.)我们无法准确预测算法 A 需要多长时间.

The only thing we can do with any reasonable expectation of success is predict that algorithm A will perform better than algorithm B (and even that will sometimes yield unexpected results for different inputs, input sizes, etc.). We cannot predict exactly how long algorithm A will take.

总结

如果没有严格控制的环境(超出本问题的范围),就不可能计算、估计甚至测量任意一段代码的运行时间.

Without an extremely controlled environment (beyond the scope of this question) it is impossible to calculate, estimate, or even measure the running time of an arbitrary piece of code.

这篇关于在已知 O(n) 和系统时钟的情况下,我们可以计算代码的执行时间吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆