| | |
Question: Which is the best function to count the running time an algorithm?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2007
Posts: 23
Reputation:
Solved Threads: 0
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:
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
or
Many thanks to whom ever decides to answer
I have to find out how many numbers can my algorithm count in 1 minute?
the code:
Java Syntax (Toggle Plain Text)
//rekursiivne algoritm public class Fibo { //Fibonacci rekursiivse algoritmi põhiosa public static long fib(int n){ if (n <= 2) return 1;//kui arv on väiksem kui kaks, siis tagastatakse "1" else return fib(n-1) + fib(n-2); } public static void main(String[] args) { int N = Integer.parseInt(args[0]);//sisestus for(int i=2; i<=N;i++){//tsükkel, mida täidetakse, kuni nõudmised on rahuldatud System.out.println(i + ": " + fib(i));//väljatrükk } }
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)
long start = System.currentTimeInMillis(); goat(); long time = System.currentTimeInMillis() - start;
Java Syntax (Toggle Plain Text)
System.currentTimeMillis()
Many thanks to whom ever decides to answer
Re: Question: Which is the best function to count the running time an algorithm?
0
#2 Sep 10th, 2007
Well, you can use if you really think you need better than millisecond resolution.
Java Syntax (Toggle Plain Text)
long startTime = System.nanoTime(); // ... the code being measured ... long estimatedTime = System.nanoTime() - startTime;
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.
•
•
Join Date: Sep 2007
Posts: 23
Reputation:
Solved Threads: 0
Re: Question: Which is the best function to count the running time an algorithm?
0
#4 Sep 11th, 2007
•
•
Join Date: Sep 2007
Posts: 23
Reputation:
Solved Threads: 0
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?
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.
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.
Java Syntax (Toggle Plain Text)
long startTime = System.nanoTime(); // ... the code being measured ... long estimatedTime = System.nanoTime() - startTime;
Java Syntax (Toggle Plain Text)
//rekursiivne algoritm public class Fibo { static final long NANO=1000L*1000*1000*1000; //long startTime=System.nanoTime(); //Fibonacci rekursiivse algoritmi põhiosa public static long fib(int n){ if (n <= 2) return 1;//kui arv on väiksem kui kaks, siis tagastatakse "1" else return fib(n-1) + fib(n-2); } //long end = 10*NANO+System.nanoTime(); //for(end<(System.nanoTime())){ public static void main(String[] args) { long end = 10*NANO+System.nanoTime(); while(end<System.nanoTime()){ int N = Integer.parseInt(args[0]);//sisestus for(int i=2; i<=N;i++){//tsükkel, mida täidetakse, kuni nõudmised on rahuldatud System.out.println(i + ": " + fib(i));//väljatrükk } System.out.print(end); } } } //startTime=startTime/10000000l; //long estimatedTime=System.nanoTime()-startTime; //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
•
•
Join Date: Sep 2007
Posts: 23
Reputation:
Solved Threads: 0
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??
In eclipse i get this kind of output:
and using command line:
So why cannot i see it in eclipse?
. Can you tell me please what i'm doing wrong?? Java Syntax (Toggle Plain Text)
//rekursiivne algoritm public class Fibo { //Fibonacci rekursiivse algoritmi põhiosa public static long fib(int n){ if (n <= 2) return 1;//kui arv on väiksem kui kaks, siis tagastatakse "1" else return fib(n-1) + fib(n-2); } public static void main(String[] args) { long start=System.nanoTime(); System.out.print(start); int N = Integer.parseInt(args[0]);//sisestus for(int i=2; i<=N;i++){//tsükkel, mida täidetakse, kuni nõudmised on rahuldatud System.out.println(i + ": " + fib(i));//väljatrükk System.out.println(System.nanoTime()); } long end=System.nanoTime(); long kulus=end-start; System.out.println(kulus); } }
Java Syntax (Toggle Plain Text)
2: 1 3: 2 4: 3 5: 5 6: 8 7: 13 8: 21 9: 34 10: 55 11: 89 12: 144 13: 233 14: 377 15: 610 16: 987 17: 1597 18: 2584 19: 4181 20: 6765
Java Syntax (Toggle Plain Text)
System time is: 1822633765464 2: 1 1822634634848 3: 2 1822634931255 4: 3 1822635194137 5: 5 1822636747128 6: 8 1822638337833 7: 13 1822639893617 8: 21 1822641384868 9: 34 1822642854329 10: 55 1822644331891 11: 89 1822645816996 12: 144 1822647326126 13: 233 1822648884425 14: 377 1822651070457 15: 610 1822653201733 16: 987 1822654758076 17: 1597 1822656452146 18: 2584 1822658018267 19: 4181 1822660821696 20: 6765 1822662514928 Elapsed time: 29478048
•
•
Join Date: Sep 2007
Posts: 23
Reputation:
Solved Threads: 0
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
So here is a littlebit changed version of the code
Java Syntax (Toggle Plain Text)
//rekursiivne algoritm public class Fibo { //Fibonacci rekursiivse algoritmi põhiosa public static long fib(int n){ if (n <= 2) return 1;//kui arv on väiksem kui kaks, siis tagastatakse "1" else return fib(n-1) + fib(n-2); } public static void main(String[] args) { long start=System.nanoTime(); start = start/10000000L; System.out.println("System time is: " +start); int N = Integer.parseInt(args[0]);//sisestus for(int i=2; i<=N;i++){//tsükkel, mida täidetakse, kuni nõudmised on rahuldatud System.out.println(i + ": " + fib(i));//väljatrükk //System.out.println(System.nanoTime()); } long end=System.nanoTime(); end = end/10000000L; long kulus=end-start; System.out.println("Elapsed time: "+kulus); } }
•
•
Join Date: Sep 2007
Posts: 23
Reputation:
Solved Threads: 0
Re: Question: Which is the best function to count the running time an algorithm?
0
#8 Sep 17th, 2007
![]() |
Similar Threads
- Time complexity of algorithm (Computer Science)
- Alogrithm Analysis (Computer Science)
- Question: Linear Time Sorting Problem (Computer Science)
- linear-time algorithm (C)
- Time Complexity ? (Computer Science)
- Write a recurrence relation for the function count. (Computer Science)
- Running Time of Recursive calls (Computer Science)
- Traveling Salesman Problem ... question about Triangle Inequality (Computer Science)
- Help me with this Question (Computer Science)
- I've got Trojan.Holax... is this bad? (Viruses, Spyware and other Nasties)
Other Threads in the Java Forum
- Previous Thread: creditcard infull +lost
- Next Thread: mock credit card with mysql & filewriter INSERT into mysql
Views: 1782 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application apps arguments array arrays automation binary bluetooth businessintelligence card chat class classes client code collision component crashcourse database draw eclipse ee error event exception file fractal free game gis givemetehcodez graphics gui helpwithhomework html ide image input integer integration j2me java javadoc javafx javaprojects jmf jni jpanel julia jvm linux list loop machine map method methods migrate mobile netbeans newbie nls number object oracle physics print problem program programming project radio recursion scanner screen security server service set size sms socket software sort sql string swing test textfield threads time transfer tree trolltech utility windows







, i don't know how to flag it like that though