Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,

I'm learning how to program in java using algorithms, my question is is there a way to know what algorithm is faster than others? How can I know an algorithm cost, and which will be the most effective. I've read a lot on the web about it but the only thing I could find is lots of weird formulas but no examples on how to implement them. For example if I have 2 algorithms : Bubble sort and Insertion sort how can I calculate the execution time of each of them and how can I know which one will be fastest and best to use. please if someone can give me an example or at least show me and understandable way to know it I'd appreciate it. or if there's a software that I could use for that purpose, that would be handy as well thanks.
Posted
Updated 28-Feb-11 14:49pm
v2

In a very general case, it is theoretically impossible to calculate executing time, because the behavior of the problem is in general case unpredictable. In particular, in general case it is even impossible to predict if the program finished execution or will run forever, as it is proven Halting problem, see http://en.wikipedia.org/wiki/Halting_problem[^], see also http://en.wikipedia.org/wiki/Computability_theory[^]. If it is impossible to tell if run time if infinite or not, it is impossible to compare time of execution of different algorithms in advance.

This is in general case. In simple cases, this is possible under certain assumption. Experiments with algorithm timing helps to get a pretty good idea on the comparisons. There enough literature on algorithm speed comparison. For comparison of sorting algorithms (http://en.wikipedia.org/wiki/Sorting_algorithm[^]), see for example: http://www.devx.com/vb2themax/Article/19900[^].

This is an interesting article on analysis of algorithms implemented in Java: http://www.cs.cmu.edu/~pattis/15-1XX/15-200/lectures/aa/lecture.html[^].

Try to Google to get more information:
http://en.lmgtfy.com/?q=comparison+speed+of+sorting+algorithms+java[^].


—SA
 
Share this answer
 
v3
Comments
Sandeep Mewara 28-Feb-11 10:52am    
Good answer!!! 5+++
Sergey Alexandrovich Kryukov 28-Feb-11 19:53pm    
Than you, Sandeep,
--SA
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.
Java
static void main(String []args) {
    //load data into an array or whatever
    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)
 
Share this answer
 
Comments
Sandeep Mewara 28-Feb-11 10:52am    
Good answer. 5+++

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900