You have a number of options.
Forgive my Java, it has been a while since I have used it.
You can add timers into your code.
long nStartTime = System.nanoTime();
Sort();
long nEndTime = System.nanoTime();
System.out.println("Execution Time: %fns", nEndTime - nStartTime);
This only works as a general guide, and cannot be relied upon.
That is because this measures the time between start and end, however multi-tasking operating systems will have switched your program out of the CPU and back in several times between the start and end of the algorithm, during which the timer is still running.
This can be observed easily by running once with nothing else running, and then again under heavy load such as converting a movie or benchmarking.
I havn't used it, but I heard that 1 of the Java IDEs (perhaps NetBeans or Eclipse) has a profiler, which you can use for profiling specific functions. This is essentially the same as the previous solution. I am not sure if this has the same problem as the solution above.
A final solution requires a Unix/Linux system, OSX may work as well.
Make a program that only contains your algorithm, and code to test it.
static void main(String []args) {
Sort();
}
Then run this code twice, the first time comment out the call to
Sort()
and run the command
time java MySort
from the Unix shell to time execution time of loading the initial data.
Next uncomment the call to
Sort()
and recompile and run the same
time java MySort
command.
The time command gives Real, System and User times.
The Real time is what you would observe with the first solution, which counts time that the process is not in the CPU.
The User time is how long it took to execute your code.
The System time is how long it took to execute system commands, including file read/write.
The total time of your algorithm is the sum of User and System times. This is what you should use for evaluating the efficiency of your script. See
Wikipedia[
^] for more details on the Unix time command.
The execution time of your algorithm is the total time of the loading sorting minus the total time of the loading only.
(user_sort + system_sort) - (user_load + system_load)