I am working in a Java project and we are not using any profiling tool.
Is there a way to find out the time a method takes for execution without using any profiling tool?
I am working in a Java project and we are not using any profiling tool.
Is there a way to find out the time a method takes for execution without using any profiling tool?
Why dont you use something like:
int startTime = System.currentTimeMillis();
methodCall();
int endTime = System.currentTimeMillis();
int totalTime = endTime - startTime;
System.out.println("Time to complete: " + totalTime);
Then you could add the /1000 or whatever to format the time as you desire.
Catch System.currentTimeMillis() before start and at the end minus with System.currentTimeMillis(). You will be able to know how much time your method takes to execute.
void fun(){
long sTime=System.currentTimeMillis();
...
System.out.println("Time Taken to execute-"+System.currentTimeMillis()-sTime+" milis");
}
Here is a sample program to capture timings:
package com.quicklyjava;
public class Main {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// start time
long time = System.nanoTime();
for (int i = 0; i < 5; i++) {
System.out.println("Sleeping Zzzz... " + i);
Thread.sleep(1000);
}
long difference = System.nanoTime() - time;
System.out.println("It took " + difference + " nano seconds to finish");
}
}
And here is the output:
Sleeping Zzzz... 0
Sleeping Zzzz... 1
Sleeping Zzzz... 2
Sleeping Zzzz... 3
Sleeping Zzzz... 4
It took 5007507169 nano seconds to finish