3 Replies - 1914 Views - Last Post: 05 November 2010 - 03:44 PM Rate Topic: -----

#1 YES i failed  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-April 10

system nano/milli time help

Posted 04 November 2010 - 05:43 PM

Ok so im trying to print to the screen how long it takes for a method to execute.
i have something like this
long start = System.nanoTime();//computation begins
       //execute method
long end = System.nanoTime();//computation ends
long time = end-start;
System.out.println(time + " nanoseconds");



example output
1809342 nanoseconds



how can i make the number display as
1.809342 milliseconds



i tried this
long time = (end-start)/1000000L;


but it rounds off to 1 digit and no decimal places.
and yes i have tried System.currentTimeMillis(); - it displays one digit as well with no decimal places.

This post has been edited by YES i failed: 04 November 2010 - 05:44 PM


Is This A Good Question/Topic? 0
  • +

Replies To: system nano/milli time help

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9038
  • View blog
  • Posts: 33,526
  • Joined: 27-December 08

Re: system nano/milli time help

Posted 04 November 2010 - 05:48 PM

You are experiencing integer division. When you divide two integer values (long, int, short, byte), the result is an integer, meaning the decmial places are truncated. Try using a double instead, and make sure the denominator is a double as well as the variable.
Was This Post Helpful? 1
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2696
  • View blog
  • Posts: 10,556
  • Joined: 15-July 08

Re: system nano/milli time help

Posted 04 November 2010 - 08:01 PM

Yep, integer division is killer. If you do this instead:

double time = (end-start)/1000000.0; // The .0 makes it a double by default.


Was This Post Helpful? 2
  • +
  • -

#4 YES i failed  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-April 10

Re: system nano/milli time help

Posted 05 November 2010 - 03:44 PM

worked perfectly guys thanks so much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1