Question: Which is the best function to count the running time an algorithm?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2007
Posts: 23
Reputation: kohuke is an unknown quantity at this point 
Solved Threads: 0
kohuke kohuke is offline Offline
Newbie Poster

Question: Which is the best function to count the running time an algorithm?

 
0
  #1
Sep 10th, 2007
I have a program that will find the certain ammount of fibonacci numbers. And now i have the question which is the best code for counting time in fibonacci algorithm?
I have to find out how many numbers can my algorithm count in 1 minute?

the code:
  1. //rekursiivne algoritm
  2.  
  3. public class Fibo {
  4. //Fibonacci rekursiivse algoritmi põhiosa
  5. public static long fib(int n){
  6. if (n <= 2) return 1;//kui arv on väiksem kui kaks, siis tagastatakse "1"
  7. else return fib(n-1) + fib(n-2);
  8. }
  9. public static void main(String[] args) {
  10. int N = Integer.parseInt(args[0]);//sisestus
  11. for(int i=2; i<=N;i++){//tsükkel, mida täidetakse, kuni nõudmised on rahuldatud
  12. System.out.println(i + ": " + fib(i));//väljatrükk
  13. }
  14.  
  15. }

and the few time counting things i have found in google. So which one suits better or can you give me a general idea where to put it?
I heard from a friend that getMills function isn't really the right one
  1. long start = System.currentTimeInMillis();
  2. goat();
  3. long time = System.currentTimeInMillis() - start;
or
  1. System.currentTimeMillis()

Many thanks to whom ever decides to answer
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,484
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 517
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Question: Which is the best function to count the running time an algorithm?

 
0
  #2
Sep 10th, 2007
Well, you can use
  1. long startTime = System.nanoTime();
  2. // ... the code being measured ...
  3. long estimatedTime = System.nanoTime() - startTime;
if you really think you need better than millisecond resolution.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Question: Which is the best function to count the running time an algorithm?

 
0
  #3
Sep 10th, 2007
For measuring the relative execution times of alternate implementations, consider turning off the HotSpot compiler, using java -Xint. This will ensure a uniform environment for the entire duration of a program.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 23
Reputation: kohuke is an unknown quantity at this point 
Solved Threads: 0
kohuke kohuke is offline Offline
Newbie Poster

Re: Question: Which is the best function to count the running time an algorithm?

 
0
  #4
Sep 11th, 2007
I'll try that java -xint thing (i usually do not use the command line to compile my code - eclipse does the work for me).
But i think that milliseconds will do fine. Thanks for the advice. i'll try that out as soon as possible.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 23
Reputation: kohuke is an unknown quantity at this point 
Solved Threads: 0
kohuke kohuke is offline Offline
Newbie Poster

Re: Question: Which is the best function to count the running time an algorithm?

 
0
  #5
Sep 11th, 2007
well i treid couple of things out. And this time this placement of functions won't get me any errors, but i can't get the time to printed out. I know that i'm doing something wrong, can you please tell me what it is?

  1. long startTime = System.nanoTime();
  2. // ... the code being measured ...
  3. long estimatedTime = System.nanoTime() - startTime;
that didn't give me any output as well(when i places the system.out.print() command there), so i did a little bit searching in google and found some codes: http://forum.java.sun.com/thread.jsp...sageID=9462589 and and edit my code with those and those still didn't give me anything good.

  1. //rekursiivne algoritm
  2.  
  3. public class Fibo {
  4. static final long NANO=1000L*1000*1000*1000;
  5.  
  6. //long startTime=System.nanoTime();
  7.  
  8. //Fibonacci rekursiivse algoritmi põhiosa
  9. public static long fib(int n){
  10. if (n <= 2) return 1;//kui arv on väiksem kui kaks, siis tagastatakse "1"
  11. else return fib(n-1) + fib(n-2);
  12. }
  13.  
  14. //long end = 10*NANO+System.nanoTime();
  15. //for(end<(System.nanoTime())){
  16. public static void main(String[] args) {
  17. long end = 10*NANO+System.nanoTime();
  18. while(end<System.nanoTime()){
  19.  
  20. int N = Integer.parseInt(args[0]);//sisestus
  21. for(int i=2; i<=N;i++){//tsükkel, mida täidetakse, kuni nõudmised on rahuldatud
  22. System.out.println(i + ": " + fib(i));//väljatrükk
  23.  
  24. }
  25. System.out.print(end);
  26. }
  27.  
  28. }
  29. }
  30.  
  31. //startTime=startTime/10000000l;
  32. //long estimatedTime=System.nanoTime()-startTime;
  33. //System.out.(estimatedTime/10000000L);

the code looks messy now, but i hope you still understand it. And yes current setting it gives me time is 10 seconds, but i thought that before i start modyfing it any different way i tought that i'd try how it works.
Last edited by kohuke; Sep 11th, 2007 at 2:52 am. Reason: forgot something
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 23
Reputation: kohuke is an unknown quantity at this point 
Solved Threads: 0
kohuke kohuke is offline Offline
Newbie Poster

Re: Question: Which is the best function to count the running time an algorithm?

 
0
  #6
Sep 17th, 2007
So now i got a program that should work, meaning that showing the time elapsed of how long the program has run. But for some reason it won't print the time out . Can you tell me please what i'm doing wrong??
  1. //rekursiivne algoritm
  2.  
  3. public class Fibo {
  4.  
  5. //Fibonacci rekursiivse algoritmi põhiosa
  6. public static long fib(int n){
  7. if (n <= 2) return 1;//kui arv on väiksem kui kaks, siis tagastatakse "1"
  8. else return fib(n-1) + fib(n-2);
  9. }
  10. public static void main(String[] args) {
  11. long start=System.nanoTime();
  12. System.out.print(start);
  13. int N = Integer.parseInt(args[0]);//sisestus
  14. for(int i=2; i<=N;i++){//tsükkel, mida täidetakse, kuni nõudmised on rahuldatud
  15. System.out.println(i + ": " + fib(i));//väljatrükk
  16. System.out.println(System.nanoTime());
  17. }
  18. long end=System.nanoTime();
  19. long kulus=end-start;
  20. System.out.println(kulus);
  21. }
  22. }
In eclipse i get this kind of output:
  1. 2: 1
  2. 3: 2
  3. 4: 3
  4. 5: 5
  5. 6: 8
  6. 7: 13
  7. 8: 21
  8. 9: 34
  9. 10: 55
  10. 11: 89
  11. 12: 144
  12. 13: 233
  13. 14: 377
  14. 15: 610
  15. 16: 987
  16. 17: 1597
  17. 18: 2584
  18. 19: 4181
  19. 20: 6765
and using command line:
  1. System time is: 1822633765464
  2. 2: 1
  3. 1822634634848
  4. 3: 2
  5. 1822634931255
  6. 4: 3
  7. 1822635194137
  8. 5: 5
  9. 1822636747128
  10. 6: 8
  11. 1822638337833
  12. 7: 13
  13. 1822639893617
  14. 8: 21
  15. 1822641384868
  16. 9: 34
  17. 1822642854329
  18. 10: 55
  19. 1822644331891
  20. 11: 89
  21. 1822645816996
  22. 12: 144
  23. 1822647326126
  24. 13: 233
  25. 1822648884425
  26. 14: 377
  27. 1822651070457
  28. 15: 610
  29. 1822653201733
  30. 16: 987
  31. 1822654758076
  32. 17: 1597
  33. 1822656452146
  34. 18: 2584
  35. 1822658018267
  36. 19: 4181
  37. 1822660821696
  38. 20: 6765
  39. 1822662514928
  40. Elapsed time: 29478048
So why cannot i see it in eclipse?
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 23
Reputation: kohuke is an unknown quantity at this point 
Solved Threads: 0
kohuke kohuke is offline Offline
Newbie Poster

Re: Question: Which is the best function to count the running time an algorithm?

 
0
  #7
Sep 17th, 2007
So anyway i fixed that not showing time problem - eclipse was just stupid and needed a restart. But can you tell me how can i get my program to work like that that it only works for a minutw? How and where do i have to write the while statent for it to do son?
So here is a littlebit changed version of the code
  1. //rekursiivne algoritm
  2.  
  3. public class Fibo {
  4.  
  5. //Fibonacci rekursiivse algoritmi põhiosa
  6. public static long fib(int n){
  7. if (n <= 2) return 1;//kui arv on väiksem kui kaks, siis tagastatakse "1"
  8. else return fib(n-1) + fib(n-2);
  9. }
  10.  
  11. public static void main(String[] args) {
  12. long start=System.nanoTime();
  13. start = start/10000000L;
  14. System.out.println("System time is: " +start);
  15. int N = Integer.parseInt(args[0]);//sisestus
  16. for(int i=2; i<=N;i++){//tsükkel, mida täidetakse, kuni nõudmised on rahuldatud
  17. System.out.println(i + ": " + fib(i));//väljatrükk
  18. //System.out.println(System.nanoTime());
  19. }
  20. long end=System.nanoTime();
  21. end = end/10000000L;
  22. long kulus=end-start;
  23. System.out.println("Elapsed time: "+kulus);
  24. }
  25. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 23
Reputation: kohuke is an unknown quantity at this point 
Solved Threads: 0
kohuke kohuke is offline Offline
Newbie Poster

Re: Question: Which is the best function to count the running time an algorithm?

 
0
  #8
Sep 17th, 2007
Problem solved , i don't know how to flag it like that though
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC