• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Quick Java question....

SokaMoka

Banned
I have a program it's almost done, however one thing missing I need to output to the user how much time each calculation took.

I know there is java.util.timer but how to use it so that it will give me the amount of time a certain block of code took to run ?

Thanks in advance.
 
The Timer class is for starting tasks at specified times, not keeping track of time passed.

This is what you do:
Get the current time. Let's say 2:30 and 15 seconds.
Do your task.
Get the current time. Let's say 2:31 and 8 seconds.

Subtract the original time from the new current time. 2:31:08 - 2:30:15 = 53 seconds that it took your task to run.
 
Originally posted by: notfred
The Timer class is for starting tasks at specified times, not keeping track of time passed.

This is what you do:
Get the current time. Let's say 2:30 and 15 seconds.
Do your task.
Get the current time. Let's say 2:31 and 8 seconds.

Subtract the original time from the new current time. 2:31:08 - 2:30:15 = 53 seconds that it took your task to run.

I understand what you said, but still you haven't alborated that into actual code.
Like let's say I have t1 ,t2 & t3

If at this moment I want to set t1 to the value of the current time in seconds, what is the actual code for that ? t1 = ?
 
import java.util.Date; // at top of file

Create two Date objects (one at the start of the test, one at the end) and find the difference in seconds with the line:

int diff = (int)( date2.getTime() - date1.getTime() ) / 1000;

The getTime() method returns the time in milliseconds, hence the need for the division by 1000.

Hope that helps.

 
wow in java 1.5 you can get it down to nano seconds....not sure why they even bothered heh. Maybe the VM is muuuuuuch faster now a nanosec makes a difference.
 
No current hardware/operating system will give you anything near nanosecond precision, even millisecond precision doesn't usually happen. I guess they're just futureproofing the api, and it's kinda neat how they worked around the fact that a long can only represent a limited timespan in nanoseconds. I suppose this is the recommended timing call (so long as you're commited to 1.5+, at least).

I'd be curious to know, though, the restraints around the "fixed but arbitrary time". Is it fixed across a single jvm (might fvck you up if you're timing distributed apps)? Is it fixed across a single compiled binary jvm (as in, they hardcoded it and will only change it when they release a new one)? Whatever they choose, it would be nice for developers to actually know what it means.

Edit: btw, I think we're talking about this: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#nanoTime()
 
System.getCurrentTimeInMillis() is what I use to time operations. It returns a long... run it once before your operation and store it in a variable. Then run it again after it's finished and store it in another variable. Subject the first from the second and you have the time it took for the operation to complete.
 
Back
Top