0

I know how to get the runtime of a method from here How do I time a method's execution in Java?

Now I need to get the run time multiple times, so is there a way to make something like

public long exTime(methodToTime())
    long startTime = System.nanoTime();
    methodToTime();
    long endTime = System.nanoTime();

    long duration = (endTime - startTime);

Thanks for your help

edit: to be more specific, my current code is

        long startTime0 = System.nanoTime();
        revealStr("1001*00*10");
        long endTime0 = System.nanoTime();
        long duration0 = (endTime0 - startTime0);
        System.out.println("The runtime is " + endTime0);
          
        System.out.println("10**01*1*0");
        long startTime1 = System.nanoTime();    
        revealStr("10**01*1*0");
        long endTime1 = System.nanoTime();
        long duration1 = (endTime0 - startTime1);
        System.out.println("The runtime is " + endTime1);
        
        System.out.println("0*1*0**0**");
        long startTime2 = System.nanoTime();
        revealStr("0*1*0**0**");
        long endTime2 = System.nanoTime();
        long duration2 = (endTime2 - startTime2);
        System.out.println("The runtime is " + endTime2);
        
        System.out.println("****1*1***");
        long startTime3 = System.nanoTime();
        revealStr("****1*1***");
        long endTime3 = System.nanoTime();
        long duration3 = (endTime3 - startTime3);
        System.out.println("The runtime is " + endTime3);
        
        System.out.println("**********");
        long startTime4 = System.nanoTime();
        revealStr("**********");
        long endTime4 = System.nanoTime();
        long duration4 = (endTime4 - startTime4);
        System.out.println("The runtime is " + endTime0);

which is repetitive and redundant

Tony Lau
  • 75
  • 6
  • Does this answer your question? [How do I write a correct micro-benchmark in Java?](https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java) – that other guy Sep 28 '20 at 22:11
  • I'm a beginner so i do not think i fully understand answers of that question but it seem not to be what I need – Tony Lau Sep 28 '20 at 22:18
  • Oh, I see what you mean. You can [see here](https://stackoverflow.com/questions/13604703/how-do-i-define-a-method-which-takes-a-lambda-as-a-parameter-in-java-8) for how to write a method that takes another method as a parameter and calls it, but obviously in this case you could just pass a String instead since the method doesn't appear to change – that other guy Sep 28 '20 at 22:28
  • seems right. thanks a lot! – Tony Lau Sep 28 '20 at 22:36
  • FYI, if the goal is to determine which function is fastest in a real application, @thatotherguy's link is absolutely critical to getting the result right. – Louis Wasserman Sep 28 '20 at 22:55

1 Answers1

1

This is a util method I used to compare the performance of two method.

public static void testPerformance(long loopTime, Runnable r1,Runnable r2){
        long startTime;
        long endTime;
        startTime=System.currentTimeMillis();
        for (int i = 0; i < loopTime; i++) {
            r1.run();
        }
        endTime=System.currentTimeMillis();
        System.out.printf("loop %d times, total spend %d s, each spend %f ms\n",loopTime,(endTime-startTime)/1000,(double)(endTime-startTime)/loopTime);

        startTime=System.currentTimeMillis();
        for (int i = 0; i < loopTime; i++) {
            r2.run();
        }
        endTime=System.currentTimeMillis();
        System.out.printf("loop %d times, total spend %d s, each spend %f ms\n",loopTime,(endTime-startTime)/1000,(double)(endTime-startTime)/loopTime);
    }

You can use like this:

PerformanceUtils.testPerformance(loopTime,()->{
        //do some thing
    },()->{
        //do some thing
    });
haoyu wang
  • 1,241
  • 4
  • 17