Author Topic: Need help optimizing a function [DONE]  (Read 665 times)

so in my day and night callbacks add-on, i have a system for dynamically determining how long a schedule should wait before running again.

this is to prevent gayass 500ms schedule loops.

You can see the function here (line 51) and the place where it is used here (line 51, heh)

E: forgot to post problem lol, editing

E: ok lets try this again

the system works by starting a while() loop and then it, starting from the current simTime, starts incrementing a fake sim time repeatedly until it reaches a fake sim time that calculates out into a different part of day than the current one

then it calculates the difference in MS from the fake sim time to the current sim time, and sets a schedule to wait until then.

key point: the system works... on kalphiters servers. on a weaker, homehosted server, the system may cause crashes, blockland hanging, timeouts, or all of the above. this is because of the goddamned inefficiency of it and because i'm bad at maff.

I need someone to help me optimize the process to meet the following criteria:

  • uses less while() iterations
  • [0]does not result in overshooting, even if it takes 700 iterations of a daycycle to be half a day off - it's not acceptable.
« Last Edit: August 14, 2012, 09:16:12 PM by Lugnut »

E: forgot to post problem lol, editing
404 problem not found

where the forget is my edit

forget you internet

this won't solve your problem, but you should use $Sim::Time instead of getSimTime() as the latter will break in math once blockland has been running for more than 999999 ms

Either way, he has a division by 1000 and using $sim::time will cut a step.

god damn guys

space guy told me that and i figured it was just a nice way to do it >.>

fine fine fine i'll do that can we focus on the problem at hand now (furling: edit at op!)

I might be missing something here, but isn't finding the part of the day and the time until the next cycle as simple as (assuming %timeIntoDay is in the range [0, %dayLength]):

Code: [Select]
%quarter = mFloatLength(%dayLength / 4, 0);
%part = mFloor(%timeIntoDay / %quarter);
%timeUntilCycle = (%part + 1) * %quarter - %timeIntoDay;


what is $donePart?
https://bitbucket.org/Tungul/event_daycycles/src/e8852a6236f6/tickSupport.cs
line 35
I might be missing something here, but isn't finding the part of the day and the time until the next cycle as simple as (assuming %timeIntoDay is in the range [0, %dayLength]):

Code: [Select]
%quarter = mFloatLength(%dayLength / 4, 0);
%part = mFloor(%timeIntoDay / %quarter);
%timeUntilCycle = (%part + 1) * %quarter - %timeIntoDay;
i added this in, i'll see how it goes

Well what I think you have to do, is figure out how much more 'fake time' it would take to be added on to get a different time of day result,

and then do a bit o' math to figure out how many times %step should be added to fake time to get as close to that as you can,

then return that number.

yes thank you ipquarx i was aware of this

my problem is right here
then do a bit o' math

lugnuts am not good at making maff

yes thank you ipquarx i was aware of this

my problem is right here
lugnuts am not good at making maff
Okay, for starters, check what $donePart is.
Then, based on that, use the code from getPartOfDayCycle to get the seconds number for when the next day cycle reaches the next quarter.

After that, you can use (i think this is right) %next * mCeil(%step / %next); to round it up as if %step was repeatedly added to get that number.

Or don't use this step bullstuff at all and calculate it straight out.

END RESULT:
i used zack0's method and it worked like a charm

thanks man

... really not sure why i didn't think it would work...