• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using currentTimeMillis correctly  RSS feed

 
Jake Mauve
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aloha! Ich bin ein noob (extrapolating from Kennedy's speech, Im assuming you can guess the meaning of the three first words))

I made a quick recursive program to test myself, on Fibonacci's, and I wanted to implement a timer. I already know which 'syntax code' to use for that, and the fact that I need to "initiate" it at start, end and get the substraction. I've gotten it to work before in another code, but here since I have two methods, Ima not sure how to do it properly. I've tried putting the code into each method (not simultaneously tho), but I keep getting the same "<identifier expected> error message when compiling. Here be my code :

public class quickFibo {
public static void main(String[]args) {

System.out.println("The 10th term of the Fibonacci sequence is "+(int)fibo(9)); } /*End main method*/

public static double fibo(int n) {

if (n<=1) return n;
else return fibo(n-1)+fibo(n-2); } /*End fibo method*/ } //End class

When searching the internet, I've found upto three different ways of defining the two first 'conditions' for the recursion, so please dont spend any time correcting this code as it works no problem. The reason I put fibo(9) instead of fibo(10) for the 10th term is to correct for first term, need to include 0 in the sequence.

So where do I stick in my two "TYPE miscVariable1 = System.currentTimeMillis();" ?
and would I then put the "System...print(miscVariable2-miscVariable1);" in the main method? its what makes most sense to me..

Thanks a lot for the help! =)
Dante

PS : as this code is the main body of the this thread, please dont make me scroll down constantly by quoting my initial message, which is already there. th-nx!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jake Mauve wrote:So where do I stick in my two "TYPE miscVariable1 = System.currentTimeMillis();" ?
and would I then put the "System...print(miscVariable2-miscVariable1);" in the main method? its what makes most sense to me..

Just before you call the fibo method, and just after you call the fibo method. After all, what's happening in the fibo method is what you want to time, isn't it?

The TYPE is going to be long, because that's what System.currentTimeMillis returns according to the API documentation.

Note that timers aren't infinitely precise; especially if you try to time things that run very quickly (in a few milliseconds or less), you might not get accurate answers. Also, doing micro-benchmarks on the JVM can be very tricky because of all the sophisticated optimizations and just-in-time compilation that the JVM does.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might do better to use the method in the System class which returns nanoseconds; I think it is called nanoTime. Last time I tried it, it appeared to give the time accurate to about 1μs. You would call that before and after fib(0) to see how long the method call on its own takes. you should have enough space in a long for a method taking several weeks to run, using nanoseconds.
 
Jake Mauve
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much to Jesper ! I finally got it to work, trying to figure out some kinks with printf for a nicer and gooder output )
I haven't tried out the nanoseconds (and apparently more 'accurate') timer yet, going to do that now. I'll be back with a point and thanks to Campbell as soon as I get it to work! =)
Thanks a lot for the quick answers! xD

PS: Java exam today went down the toilet, but I'll keep trying! xD
 
Jake Mauve
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much to both Campbell Ritchie and Jesper de Jong !
Both answers were very helpful and useful ! Both worked fine.
Just to recap:

you can use two methods to get a 'timer' of sorts going:
in milliseconds using "System.currentTimeMillis();" :
long timeVariable1 = System.currentTimeMillis();
System.out.println("\n\t The "+N+"th term of the Fibonacci sequence is "+(int)fibo(N-1));
long timeVariable2 = System.currentTimeMillis();

and I suggest adding: double Times=(double)(timeVariable2-timeVariable1)/1000;
//to get the result directly displayed in seconds, and in double (with cast to double) for the decimals



and in nanoseconds, more precise, using "System.nanoTime();" :
long startTime = System.nanoTime();
System.out.println("\n\t The "+N+"th term of the Fibonacci sequence is "+(int)fibo(N-1));
long endTime = System.nanoTime();

same suggestion as previous: double Times=(double)(endTime-startTime)/1000000000; //--> seconds, dont forget nano means 10 to the power of -9


and then finish off both with a System.out.println("\nTime: "+Times);
or System.out.printf("\nTime: %.2f\n",Times); //--> two decimal points after 0


PS: can someone please correct me if I'm mis-using double type? I tend to use it as often as possible
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are misusing double. Remember whenever you stray into floating-point land, you relinquish any pretence of precision. You don’t need most of those casts. You probably don’t need any. Don’t cast your longs to ints. You can print a long.
Find about the % tags, and why you ought not to use \n, even though it is in all the books.You probably don’t need it at this stage, but there is a TimeUnit enum which may be able to convert seconds to milliseconds, etc.
 
Jake Mauve
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im still a beginner. I stick with double cause it makes some basic things easier for me, and out of the logic of 'why not use the biggest range of numbers possible?' plus I still don't know enough of the different concepts and rules to be able to have a handle on how to mix types and variables around and things ) - at this point its more "int for integers and double for decimal values, and then string for non numbers" =)
and why use the % sign instead of \ for "\n vs %n" ? are they exactly the same(or not)? I dont mind using %n over the other, I just need to know why Im doing it^^. It wasn't explained on the site you linked )
haven't gotten as far as enum's yet =) I'll keep them for later. I'm more or less just one small step passed the very basic fundamentals. Like, I can initiate arrays, but haven't gotten to any complex manipulation thereof, to make basic games for example. At this point, I still don't really see how I can fit a tic-tac-toe game into arrays :p And recursion I finally understood properly the same evening I posted my original post =) I could do it before, but not without help. The other day, the bulb finally went on and it clicked. Before that, I had heard my teacher and fellow students (and tutorial ppl) repeating the same thing 'its a method calling itself, seemingly infinite until it reaches base case'.. and to me that was just as mystical as blue ketchup :p (just trying to better explain where exactly I'm at) ^^ Im still behind on object manipulation, constructors (I can write em but I still don't see the bigger picture yet) and for example the simplest thing like defining separate methods. Except for my fibonacci program, everything else was simple enough to keep under the main method heading.

Thanks again for all the help! =)
Cheers
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jake Mauve wrote:. . . why use the % sign instead of \ for "\n vs %n" ? are they exactly the same(or not)? . . .
Did you read the link in my last post about % tags? That answers your question. We can give more details if you want them.
The other day, the bulb finally went on and it clicked. . . .
Isn’t it marvellous when that happens
blue ketchup . . .
Is that some pornographic thing? We’re not that sort of website
thanks again for all the help! =)
Cheers
You’re welcome
 
Jake Mauve
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the full quote of that first line you referred to was :



"and why use the % sign instead of \ for "\n vs %n" ? are they exactly the same(or not)? I dont mind using %n over the other, I just need to know why Im doing it^^. It wasn't explained on the site you linked )"



read that last part.. notice how it also was in the original. and no, blue ketchup is ketchup which is blue .. the link to pornography is far stretched, seems more like one of those typically 'nervous' jokes insecure people make to fill in the void. 'marvelous when those happen'..
but if you don't know, then by all means, lets post another combined 3-4 messages to finally come to the conclusion of what its for. Cause if you do know why you're using %n instead of the other option, wouldnt it be smarter for you to explain me the reason in your words then go through the trouble of finding a site and copy pasting the URL? Unless you have no clue as to why you're doing it of course.

Bear, could you please close this thread its served its purpose.
(I'm deactivating email notif's for this thread so type away but it'll be a waste)
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need to be sarcastic, thank you very much.

The link I quoted says
A new line character appropriate to the platform running the application. You should always use %n, rather than \n.
The \n character is not always the character appropriate to the platform. There is not a lot more to explain. There are actually several pages in the Tutorials about formatting, and I am sorry, but I think there would have been better details in the other one, which says
In the Java programming language, the \n escape always generates the linefeed character (\u000A). Don't use \n unless you specifically want a linefeed character. To get the correct line separator for the local platform, use %n.
Some well-known platforms may use \n as a line terminator (*nix, including Linux and newer Macs), or \r (old Macs; that use is more-or-less obsolete nowadays), and some others use \r\n, ie two characters (DOS and Windows). Most applications can compensate, but you try writing output with \n rather than %n on a Windows box, and opening the output with NotePad, and you can see what happens to an application which doesn’t cope well with different line ends.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!