This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Finding Elapsed Time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Finding Elapsed Time" Watch "Finding Elapsed Time" New topic
Author

Finding Elapsed Time

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1394
I need to find the elapsed mili seconds between the start of a procedure and its finish. I use this number to append to a file path so as to not diplicate file names. My code is here but I get a result of zero.

Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
I usually do it this way. Hope this helps.

Jim ... ...


BEE MBA PMP SCJP-6
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

So that tells you that the code took less than a millisecond to run.

This probably isn't surprising if you just timed a single run of a single method. You're running, let's say, a 1 gigahertz machine. That means it can execute 10^9 machine operations in a second. This is 10^6 -- one million -- machine operations in a millisecond. (Probably your machine is faster than that, but the arithmetic is harder.) So what you timed took less than one million machine operations.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1394
Jim Hoglund wrote:I usually do it this way. Hope this helps.

Jim ... ...


This looks like it should work but I am still getting zero. The process runs very fast but surely there is some fraction of time difference.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1394
Paul Clapham wrote:So that tells you that the code took less than a millisecond to run.

This probably isn't surprising if you just timed a single run of a single method. You're running, let's say, a 1 gigahertz machine. That means it can execute 10^9 machine operations in a second. This is 10^6 -- one million -- machine operations in a millisecond. (Probably your machine is faster than that, but the arithmetic is harder.) So what you timed took less than one million machine operations.


Okay, that makes sense. So my altrernative is a timestamp but how can I get the result of the following to appear like a simple string?



Results: 2011-06-28 17:24:53.911

A simple replace?
Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 174

There may be a more elegant way, but here is what I have...

Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
How about:
Jim ... ...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Instead of new Date().getTime() use System.currentTimeMillis(). They return the same value without the need for a new Date object.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
. . . or the System class method which returns time in nanoseconds. It's called something like nanoTime or getNanoTime.
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Excellent !
Jim ... ...
lokesh sree
Ranch Hand

Joined: Oct 27, 2009
Posts: 99

Hi Steve,

You can do the following..

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

And hope the code takes atleast nano seconds to execute..


Lokesh
( SCBCD 5, CCENT, SCJP 5 )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Finding Elapsed Time
 
Similar Threads
What fields does java.util.Date.equals compare?
how to find how many sundays comes in 2 Dates
Subtract 2 Dates
One More to the K&B Errata log...Ch 6 Dates related
please help me ...