aspose file tools
The moose likes Beginning Java and the fly likes [SOLVED] Is it my math or my logic/code? Number Formatting difficulties Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "[SOLVED] Is it my math or my logic/code? Number Formatting difficulties" Watch "[SOLVED] Is it my math or my logic/code? Number Formatting difficulties" New topic
Author

[SOLVED] Is it my math or my logic/code? Number Formatting difficulties

Brian LaRue
Ranch Hand

Joined: Feb 01, 2006
Posts: 45
Hey all, I can't decide what's going wrong here. I'm trying to write a program (for an assignment at school) which times how long the process takes to search a list. I can get the program to run, but I'm getting unexpected results. It's always telling me that it takes 0 ms to run. I don't know if it's my math, the formatting of the result, or somewhere in my program logic that's producing these results. Here's what I've got so far:



Where could it be? Ive read the javadocs and discovered that currentTimeMillis() returns a long, so Ive declared the variables as such (to be large enough to handle them). I'm sure there's a better collection, implementation but as this is for an assignment, I have no choice but to use this particular set up. Either that or is my Core2 Duo laptop so smoking that it takes a nanosecond to locate it?

TIA

[ February 26, 2008: Message edited by: Brian LaRue ]
[ February 26, 2008: Message edited by: Brian LaRue ]
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

At first sight it looks like there's nothing wrong with your program.

You're looking up the product in an array of strings that contains 19 elements. This most likely takes only a fraction of a millisecond, that's why you see "0 ms". Note that the precision of the timer that you query with System.currentTimeMillis(); depends on the operating system. For some versions of Windows, the precision can be as low as about 55 ms - so if you do anything that takes less than 55 ms, you can't time it accurately with this timer.

If you're using Java 5 or newer, you could try using System.nanoTime(), which possibly uses a more precise timer.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Brian LaRue
Ranch Hand

Joined: Feb 01, 2006
Posts: 45
Well thanks for the suggestion of the System.nanoTime() method. I was underwear of that one and now I'm getting something other than "0 ms", which is what I ultimately wanted. It's giving me an output of: "Total Time: 781359 ms". I know the ms isn't true but that's an easy fix. The actual time changes depending on which product I choose to search for. That's neat!

How can I convert the nano seconds to seconds? I'm getting a possible loss of precsion here. I'm guessing that you have to cast the long down into a double? Or would float be more appropriate? I would like it to say "Total Time: 0.000781359 seconds". I read that a nano second is like multiplying your answer by 0.000000001 or 10^-9.



gives me the "possible loss of precision" compiler error. What am I doing wrong now?

Thanks again
Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
Try using BigDecimal. Its made just for this occasion

Try looking here...

Hope this helps.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32611
    
    4
Alternative to BigDecimal:You can alter the numbers to get milliseconds or microseconds.
Brian LaRue
Ranch Hand

Joined: Feb 01, 2006
Posts: 45
Sweet! Thanks, I tried the printf() while tailoring it to my specific needs, it works! Thanks for everyone's help.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: [SOLVED] Is it my math or my logic/code? Number Formatting difficulties
 
Similar Threads
why use int over double?
NIO vs IO
Copying Object (such as Button, Label, and personal Objects) ?
How do you evaluate getTime() repeatedly?
Revere elements