Thread Tools Display Modes
01-18-12, 06:00 AM   #1
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
OnUpdate efficiency

Hello!

I use an OnUpdate script to display fps/latency/hour+minutes in my UI. It updates every second, mainly for the fps.

I was wondering though. Considering latency is only updated every 10 seconds, and there is no use in getting for the time every second when it only updates every minute, would it be more efficient to use 3 separate OnUpdate loops - one which updates the hour/minute value every 30 secs, one which updates the latency every 10 secs, and one which handles the fps as well as displaying the other values - or should I stick to just 1?

The difference is minimal, of course, it's mostly a matter of principle.

Thanks!
  Reply With Quote
01-18-12, 07:04 AM   #2
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 793
So either one update that runs a function or two updates running two separate functions, good question.

Oh, I just wish you could make onupdate scripts and specify the update interval when you set the script on the widget, think it would be more performance friendly like in scenarios like this yes? Having a bunch of variables to count in order to track the time elapsed, too many of those and you get impact on performance... oh sorry off topic!
  Reply With Quote
01-18-12, 07:55 AM   #3
Barjack
A Black Drake
AddOn Author - Click to view addons
Join Date: Apr 2009
Posts: 89
My intuition would be that function call overhead makes a single OnUpdate function (marginally) more efficient--though I haven't actually tested this.

But since OnUpdate scripts on visible frames are called once per frame regardless of whether or not your condition in that function evaluates true or false, the result in either case is 3 mathematical operations per frame (adding time elapsed to a "time since last update" for each type of thing), 3 comparisons per frame (comparing total time elapsed against your delay for that type), but 1-3 function calls per frame depending on the number of functions you're using.

Again though, this is all just assumptions on my part. I've not actually benchmarked the difference and there may very well be considerations I'm overlooking. And if frames are ever being hidden and their OnUpdate scripts therefore skipped, that may make a difference as well.

Last edited by Barjack : 01-18-12 at 07:57 AM.
  Reply With Quote
01-18-12, 07:57 AM   #4
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
I had an idea, not sure why I didn't think of this before - and I think it is what Barjack means also - it would be possible to use 3 different 'time since last update' values, check for each in one OnUpdate script and let them count to different numbers. That might be the most efficient, unless conditions actually require more instructions than a simple time/latency check.
  Reply With Quote
01-18-12, 08:07 AM   #5
Barjack
A Black Drake
AddOn Author - Click to view addons
Join Date: Apr 2009
Posts: 89
Originally Posted by Haleth View Post
I had an idea, not sure why I didn't think of this before - and I think it is what Barjack means also - it would be possible to use 3 different 'time since last update' values, check for each in one OnUpdate script and let them count to different numbers. That might be the most efficient, unless conditions actually require more instructions than a simple time/latency check.
That's how I was assuming the single-OnUpdate version would work, yeah. Something like

Code:
function someUpdateChecker (self, elapsed)
  local timer1 = timer1 + elapsed
  local timer2 = timer2 + elapsed
  if timer1 > limit1 then
    timer1 = 0
    doStuff1()
  end
  if timer2 > limit2 then
    timer2 = 0
    doStuff2()
  end
end
Which is what I assume is more efficient than a 3-function (or 2-function in this simplified example) alternative simply because it essentially is 3 different update functions, just all crammed into one. Perhaps you had something else in mind, though.

Naturally, as you said in the original post, the difference is probably undetectably minor. In fact, I wouldn't be surprised to learn that the speed of whatever has to manage SetScript callbacks etc. is slower than the actual difference here and therefore results in a slightly bigger difference overall.

Again, all completely untested etc.
  Reply With Quote
01-18-12, 08:12 AM   #6
Nimhfree
A Frostmaul Preserver
AddOn Author - Click to view addons
Join Date: Aug 2006
Posts: 267
My first assumption is that you should code everything up in just one. Ignore the timing of FPS and the actual clock and just update all of them each time you decide to do work in your OnUpdate script. If that works well and does not slow things down you are fine. If you are really worried about it, you can time the individual calls and determine if their overhead is worth the mathematical comparisons needed to control accessing them.
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » OnUpdate efficiency


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off