943,808 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2435
  • Java RSS
Sep 10th, 2007
0

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

Expand Post »
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:
Java Syntax (Toggle Plain Text)
  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
Java Syntax (Toggle Plain Text)
  1. long start = System.currentTimeInMillis();
  2. goat();
  3. long time = System.currentTimeInMillis() - start;
or
Java Syntax (Toggle Plain Text)
  1. System.currentTimeMillis()

Many thanks to whom ever decides to answer
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kohuke is offline Offline
23 posts
since Sep 2007
Sep 10th, 2007
0

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

Well, you can use
Java Syntax (Toggle Plain Text)
  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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Sep 10th, 2007
0

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

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 11th, 2007
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kohuke is offline Offline
23 posts
since Sep 2007
Sep 11th, 2007
0

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

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?

Java Syntax (Toggle Plain Text)
  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.

Java Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kohuke is offline Offline
23 posts
since Sep 2007
Sep 17th, 2007
0

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

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??
Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kohuke is offline Offline
23 posts
since Sep 2007
Sep 17th, 2007
0

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

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
Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kohuke is offline Offline
23 posts
since Sep 2007
Sep 17th, 2007
0

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

Problem solved , i don't know how to flag it like that though
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kohuke is offline Offline
23 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: creditcard infull +lost
Next Thread in Java Forum Timeline: mock credit card with mysql & filewriter INSERT into mysql





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC