Quick Java question....

SokaMoka

Banned
Feb 24, 2006
521
1
0
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.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
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.
 

SokaMoka

Banned
Feb 24, 2006
521
1
0
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 = ?
 

professor1942

Senior member
Dec 22, 2005
509
0
0
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.

 

Pens1566

Lifer
Oct 11, 2005
13,120
10,477
136
java.lang.system.getCurrentTimeInMillis

solution

Date is deprecated, and using a GregorianCalendar is waaaayyyy to heavyweight a solution. Enjoy.
 

meksta

Senior member
Jul 24, 2001
252
0
0
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.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
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()
 

clamum

Lifer
Feb 13, 2003
26,252
403
126
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.