Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

java - With O(n) known and system clock known, can we calculate the execution time of the code

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");
    }
 }

The output was 855 nanoseconds.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

Summary

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...