Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Finding Elapsed Time

 
Ranch Hand
Posts: 2003
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I usually do it this way. Hope this helps.

Jim ... ...
 
Marshal
Posts: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2003
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 2003
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Saloon Keeper
Posts: 7175
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There may be a more elegant way, but here is what I have...

 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about:
Jim ... ...
 
Sheriff
Posts: 21949
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of new Date().getTime() use System.currentTimeMillis(). They return the same value without the need for a new Date object.
 
Marshal
Posts: 69420
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . or the System class method which returns time in nanoseconds. It's called something like nanoTime or getNanoTime.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent !
Jim ... ...
 
Ranch Hand
Posts: 100
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
rubbery bacon. crispy tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic