Timing method inside multiple for in java

65 views Asked by At

i am having trouble with timing two methods that are included inside multiple for. I am using System.currentTimeMillis(); . The problem is that i don't know where to put start time and end time. I want to time 5 executions of methodA and methodB. When i run it i get 12 ms for methodA and 0ms for methodB but the program takes about 10 minutes to run. The code is like below:

     int count = 0;
     double e = 0.3;
     long startTimeA;
     long endTimeA;
     long startTimeB;
     long endTimeB;
     long sumA = 0;
     long sumB = 0;

     for(int k=0; k<5; k++){
        startTimeA = System.currentTimeMillis();
        startTimeB = System.currentTimeMillis();
        for(int i = 0; i < 40; i++) {
            for(int j = i + 1; j < 40; j++) { 
               double a = methodA();
               endTimeA = System.currentTimeMillis() - startTimeA;
               double b = methodB();
               endTimeB = System.currentTimeMillis() - startTimeB;

               int c = methodC();

            }
            sumA += endTimeA;
            sumB += entTimeB;
     }
  }
  System.out.println("total time A: " + sumA + " ms");
  System.out.println("total time B: " + sumB + " ms");
3

There are 3 answers

2
ninja.coder On BEST ANSWER

You should read about the micro-benchmarking and how to do it in practice.

Here is a way to accomplish what you are trying to do:

private void doSomething() {
    long startTimeA = System.currentTimeMillis();
    executeA();
    long endTimeA = System.currentTimeMillis();
    System.out.println("Total Time A: " + (endTimeA - startTimeA));

    long startTimeB = System.currentTimeMillis();
    executeB();
    long endTimeB = System.currentTimeMillis() - startTimeB;
    System.out.println("Total Time B: " + (endTimeB - startTimeB));
}

private void executeA() {
    for(int k=0; k<5; k++) {
        for(int i = 0; i < 40; i++) {
            for(int j = i + 1; j < 40; j++) { 
               double a = methodA();
            }
        }
    }
}

private void executeB() {
    for(int k=0; k<5; k++) {
        for(int i = 0; i < 40; i++) {
            for(int j = i + 1; j < 40; j++) { 
               double b = methodB();
            }
        }
    }
}
1
RAZ_Muh_Taz On
        for(int k=0; k<5; k++){
            sumA = 0;
            sumB = 0;
            for(int i = 0; i < 40; i++) {
                for(int j = i + 1; j < 40; j++) {
                    startTimeA = System.currentTimeMillis();
                    double a = methodA();
                    endTimeA = System.currentTimeMillis() - startTimeA;
                    sumA += endTimeA;

                    startTimeB = System.currentTimeMillis();
                    double b = methodB();
                    endTimeB = System.currentTimeMillis() - startTimeB;
                    sumB += endTimeB;

                    int c = methodC();
                }
         }
         System.out.println("total time A: " + sumA + " ms");
         System.out.println("total time B: " + sumB + " ms");  
      }
0
Dmitry Gorkovets On

Just move

sumA += endTimeA;
sumB += entTimeB;

into "for j" loop.