Author |
Message |
CypherJF I gargle nitroglycerin

Gender: Joined: Aug 14 2003 Posts: 2582 Location: USA Offline
|
|
Back to top |
|
 |
Cyan~Fire I'll count you!

Age:37 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
Posted: Wed Jun 22, 2005 11:39 pm Post maybe stupid Post subject: |
 |
|
|
|
Eh, Java sucks. Hey, I'm going to hijack your topic. Anybody know of an easy way to do function timing in Java? _________________ This help is informational only. No representation is made or warranty given as to its content. User assumes all risk of use. Cyan~Fire assumes no responsibility for any loss or delay resulting from such use.
Wise men STILL seek Him. |
|
Back to top |
|
 |
Cerium Server Help Squatter

Age:43 Gender: Joined: Mar 05 2005 Posts: 807 Location: I will stab you. Offline
|
Posted: Thu Jun 23, 2005 3:32 am Post maybe stupid Post subject: |
 |
|
|
|
Well, the Timer class doesnt do anything that cant be duplicated. Since a fix may be a while off (and would no doubt break backwards comatibility with previous versions of java), youre left writing a custom Timer class.
You could copy/paste most of the code directly from Timer, but youd need to change the sched code. The bug page you posted already has a fairly good example of a workaround for the time being set backwards, but it needs to be updated.
wait time = ( Math.min( <calculated wait time>, <length between most frequent task executions>) );
That way at most, it would only wait the amount of time specified by the task that executes the most often.
Ignoring forward skips may be a bit harder, since a timer task could end up making the timer sleep for a very long time. Perhaps change the timer body it knows how long each iteration takes. Then, only sleep for a maximum of say 10 seconds each time it sleeps. If the 'iteraiton' time is longer than 10 seconds, you can assume the clock was set forward, and handling it accordingly.
The downside, is this may make some timers off by a few milliseconds, but hell, the Timer class shouldnt be used when you need tasks to be that accurate, anyway.
---
Cyan, I dunno if this is the best, or easiest, but this is what I use:
int intChecks = 50;
int intIterations = 25000;
long[] lngDuration = new long[intChecks];
double[] dblRuntime = new double[intChecks];
DecimalFormat objFormat = new DecimalFormat("#.#####");
// Setup variables and stuff here
for(int intUpper = 0; intUpper < intChecks; ++intUpper)
{
long lngStart = System.currentTimeMillis();
for(int intCounter = 0; intCounter < intIterations; ++intCounter)
{
// Code to time goes here
}
lngDuration[intUpper] = System.currentTimeMillis() - lngStart;
dblRuntime[intUpper] = (double) lngDuration[intUpper] / (double) intIterations;
System.out.println("Duration: " + lngDuration[intUpper]);
System.out.println("Runtime: " + objFormat.format(dblRuntime[intUpper]));
}
long lngTotal = 0;
double dblTotal = 0;
for(int intCounter = 0; intCounter < intChecks; ++intCounter)
{
lngTotal += lngDuration[intCounter];
dblTotal += dblRuntime[intCounter];
}
System.out.println("-----");
System.out.println();
System.out.println("Avg Duration: " + (lngTotal / intChecks));
System.out.println("Avg Runtime: " + objFormat.format((dblTotal / intChecks)));
|
It gets more and more accurate the longer its allowed to run, but if anyone knows an even better way, lemme know =) _________________ There are 7 user(s) ignoring me right now. |
|
Back to top |
|
 |
Dr Brain Flip-flopping like a wind surfer

Age:39 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
Posted: Thu Jun 23, 2005 7:21 am Post maybe stupid Post subject: |
 |
|
|
|
Can't you just make yourself a Thread that has a sleep(10000); at the end of run()? _________________ Hyperspace Owner
Smong> so long as 99% deaths feel lame it will always be hyperspace to me |
|
Back to top |
|
 |
Cerium Server Help Squatter

Age:43 Gender: Joined: Mar 05 2005 Posts: 807 Location: I will stab you. Offline
|
Posted: Thu Jun 23, 2005 8:18 am Post maybe stupid Post subject: |
 |
|
|
|
Would work fine if your app only requires the one task. But when you start adding lots of tasks that need to be done at different intervals, you start running into problems. |
|
Back to top |
|
 |
Bak ?ls -s 0 in

Age:26 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: Thu Jun 23, 2005 10:07 am Post maybe stupid Post subject: |
 |
|
|
|
perhaps a cool linkedlist with sleep times and methods to run when the time runs out... so like If I wanted to run something in 10 seconds and something else in 7 seconds it would look like:
(7, func1) -> (3, func2) ->
then after func1 runs it could reschedule itself to run 10 seconds later which would be inserted into the linkedlist as:
(3, func2) -> (7, func1) -> _________________ SubSpace Discretion: A Third Generation SubSpace Client |
|
Back to top |
|
 |
CypherJF I gargle nitroglycerin

Gender: Joined: Aug 14 2003 Posts: 2582 Location: USA Offline
|
|
Back to top |
|
 |
Cerium Server Help Squatter

Age:43 Gender: Joined: Mar 05 2005 Posts: 807 Location: I will stab you. Offline
|
Posted: Thu Jun 23, 2005 1:26 pm Post maybe stupid Post subject: |
 |
|
|
|
Well, sleep is a native function, so I would hope that its a bit more safe. But even still, it could be a race condition of sorts, in that when you were changing the time, you were getting lucky and the time was getting updated during the System.out.println call...
And bak, what you described is basicly how Timer works. Theres a list of objects to run. After an object is run, it iterates through the list and finds the object that should be run next. It uses System.currentTimeMillis() - (next runtime) to determine how far off the execution is, then sleeps. |
|
Back to top |
|
 |
CypherJF I gargle nitroglycerin

Gender: Joined: Aug 14 2003 Posts: 2582 Location: USA Offline
|
Posted: Thu Jun 23, 2005 2:31 pm Post maybe stupid Post subject: |
 |
|
|
|
I don't trust the exact Date print, but eh for all I care I could have just had an incremental integer going. Its just a way to see that it was doing it's thing.
But I still find it funky the above Runnable code didn't work but then it now works. :shrug:. And it didn't work prior either because I had writen my own timer to throw "CypherTimerEvent" . But the thread wasn't executing right. :shrug: |
|
Back to top |
|
 |
Dr Brain Flip-flopping like a wind surfer

Age:39 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
|
Back to top |
|
 |
|