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:

``````//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);//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 :(

``````long start = System.currentTimeInMillis();
goat();
long time = System.currentTimeInMillis() - start;``````

or

``System.currentTimeMillis()``

Many thanks to whom ever decides to answer

Well, you can use

``````long startTime = System.nanoTime();
// ... the code being measured ...
long estimatedTime = System.nanoTime() - startTime;``````

if you really think you need better than millisecond resolution.

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.

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.

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?

``````long startTime = System.nanoTime();
// ... the code being measured ...
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.jspa?threadID=782432&messageID=9462589 and and edit my code with those and those still didn't give me anything good.

``````//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);//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.

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??

``````//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);//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);
}
}``````

In eclipse i get this kind of output:

``````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``````

and using command line:

``````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``````

So why cannot i see it in eclipse?

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

``````//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);//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);
}
}``````

Problem solved:) , i don't know how to flag it like that though

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.