|
Author |
Topic: framerate timer (Read 411 times) |
|
Euskadi
|
framerate timer
« on: Jan 24th, 2004, 6:17pm » |
|
I'm writing a simulation that should run as fast as possible without slowing down the framerate... things slow down if I pack too many iterations into each frame. I have written this timer that tracks the average time of the last 'p' loops in millis. If the timer says things are fine I boost the number of iterations per frame. If the timer says things are slow I decrease the number of iterations. It seems to work, but I'm wondering if there's a better method that someone has written... and maybe this belongs in the suggestions forum, but it sure would be cool if Processing had something like this built in. class timer{ private int[] points; private int pCount; // number of loops to average private int tCount; // number of loops tracked when filling private int lastTick; // millis() of last tick private boolean filling; // true if timer just started and // pCount loops have not happened yet private boolean on; // on or off? private int pTotal; //sum of all points //constructor, create timer to track average time of last p loops timer(int p){ points = new int[p]; for(int i=0; i<p; i++){ points[i]=0; } } // return the average of all points without creating new point int speed(){ int temp; if(filling){ if(lastTick < 0) {temp = 0;} else {temp = pTotal / (tCount + 1);}} else {temp = pTotal / pCount;} return temp; } //start the timer if it isn't going already; void start(){ if(on != true){ lastTick = millis(); tCount = -1; pTotal = 0; } } void stop(){ on = false; } // tick the timer once each loop to mark the time // return the time for the last p loops if on, or // return -1 if the timer was off int tick(){ if(on){ int t = (millis()-lastTick); pTotal = t - points[tCount]; points[tCount]= t; tCount++; if(tCount==pCount){ tCount = 0; filling = false; } return speed(); } else { start(); return -1; } } }
|
|
|
|
arielm
|
Re: framerate timer
« Reply #1 on: Jan 24th, 2004, 8:52pm » |
|
do you take in count the following?: between each execution of the code within loop(), the processing engine is sending the pixels[] array to the screen, then eventually clears the pixels[] and zbuffer[] arrays, then yields (i.e. gives some time to other tasks in the system to execute), all these steps are taking time to execute (with a more or less equal duration, specific to each computer, providing you're not doing something else with the system at the same time...) some questions: - what is your simulation about? - what framerate do you want to achieve?
|
Ariel Malka | www.chronotext.org
|
|
|
Euskadi
|
Re: framerate timer
« Reply #2 on: Jan 24th, 2004, 10:28pm » |
|
i'm trying to simulate a client/server environment where there are a few thousands requests per hour and a queue when all servers are busy. i do understand that there is some fixed overhead that happens each loop regardless the number of iterations that i include in each loop... i'm ticking the timer at the very beginning of each loop. i also use the int speed() method at the end of each loop() to get the time of each loop (my stuff), and then I estimate the P5 overhead so I know roughly how much of the timer() time I can actually impact and how much is out of my control. i'm shooting for 24 fps, it seems like a nice balance to limit the amount of overhead time but to also maintain decent responsiveness from a couple controls i have in the app, including a slider bar that i use to let the user dictate the target rate. ultimately i'm trying to run the app up to the user's limit, so long as the framerate does not go below 24 fps.
|
|
|
|
arielm
|
Re: framerate timer
« Reply #3 on: Jan 25th, 2004, 12:27am » |
|
i don't know if it's relevant, but couldn't your simulation be achieved using a different strategy, involving multi threading? (well, i'm aware it's not compatible with the processing way of working...) e.g. one thread running some iterations and another thread for displaying results on the fly... btw, your simulation reminds me of the Sand Pile Model (SPM) used in computer science to balance single processor overhead within large multi processors machines (personally, i use the SPM to actually simulate sand, but it's another story...)
|
Ariel Malka | www.chronotext.org
|
|
|
Euskadi
|
Re: framerate timer
« Reply #4 on: Jan 25th, 2004, 11:48pm » |
|
You raise a good point... I haven't actually looked at the difference in time between my activities and the loop's screen writing activities. Is P5 doing any multi-threading so that subsequent loop processing can happen while the previous loops are still writing to the screen? Even if that were the case though.... it would only negate my need to track P5's overhead time. I would still need a way to make sure I'm packing enough activity into each loop without slowing my framerate.
|
|
|
|
fry
|
Re: framerate timer
« Reply #5 on: Jan 26th, 2004, 4:40am » |
|
you oughta just be able to use the framerate() function, which will return your average framerate (integrated over the last 10 or so frames) and then increment or decrement the amount of data that you're chewing on accordingly. however, watch out to slowly increment and decrement the data that's being dealt with, since a short hiccup in the framerate might cause your program to get confused.
|
|
|
|
Euskadi
|
Re: framerate timer
« Reply #6 on: Jan 27th, 2004, 1:45am » |
|
THANK YOU. Suggestion for the website then... it would be lovely if the framerate reference page stated this explicitly. I did run across another question about framerate that stated that framerate() returns the framerate, but it is unclear whether it returns the programmer's framerate setting or the actual framerate experienced. Thanks again!!
|
|
|
|
|