ID:262302
 
Code:
proc
AgeCheck()
if(sec == 60)
min += 01
sec -= 60
if(min == 60)
hrs += 01
min -= 60
if(hrs == 24)
day += 01
hrs -= 24
Aging()
Aging()
sleep(10)
sec += 01
AgeCheck()


New()
..()
Aging()


Problem description:
I think I need a new better proc because I know this isn't very good. It was supposed to be a fill-in until I figure out a better one but Dream Seeker causes an error after about 3 minutes of running this one. Anyone got an Idea for a better one?

What error does it cause?

Prodigal Squirrel
In response to Prodigal Squirrel
proc
AgeCheck()
if(sec >= 60) //Incase it doesn't go above
min += 01
sec -= 60
if(min >= 60) //Same
hrs += 01
min -= 60
if(hrs >= 24) //Same here too
day += 01
hrs -= 24
Aging()
while(1) //While loops are teh fun.
sleep(10)
sec += 01
AgeCheck()

world // o.O New was all alone, I think you wanted world/New()
New()
..()
Aging()


Also, you may want to use ++ instead of +=1, but that's up to you.
It should work now.
In response to Hell Ramen
This will cause the same problem as the original. By having a proc call itself (which will then call itself again, endlessly until it is either stopped for being an infinite loop, or it eats up all available memory and freezes (which will only happen if you set it to background or disable loop_checks, both of which are bad and you shouldn't have to do).

To fix the original way, use spawn() AgeCheck(). This causes a completely independent instance of AgeCheck() to start.

To fix the example using the while loop, simply remove AgeCheck() from within the while loop.
proc
Age()
seconds++
minutes += round(seconds/60)
seconds %= 60 //modulo, 59 % 60 = 59; 60 % 60 = 0.
hours += round(minutes/60)
minutes %= 60
days += round(hours/24)
hours %= 24
spawn(10)
Age()


There, that's much more efficient. No more if trees!

Modulo and round()ed division makes this kind of thing so much easier!
In response to Ter13
Did you even read my previous reply to your bogus effeciency claims :P? [link]

Anyway if you don't believe me here's some test code to demonstrate.

var
sec
min
hrs
day
seconds
minutes
days
hours

proc
Age1()
sec += 01
if(sec == 60)
min += 01
sec -= 60
if(min == 60)
hrs += 01
min -= 60
if(hrs == 24)
day += 01
hrs -= 24
Age2()
seconds++
minutes += round(seconds/60)
seconds %= 60 //modulo, 59 % 60 = 59; 60 % 60 = 0.
hours += round(minutes/60)
minutes %= 60
days += round(hours/24)
hours %= 24

mob/verb/StartTest()
var/const/NUMTESTS = 100000
for(var/i = 0; i < NUMTESTS; i++)
Age1()
for(var/i = 0; i < NUMTESTS; i++)
Age2()


The profiling results on my machine were

                        Self CPU Time   Total CPU Time  Real Time   Calls
/mob/verb/StartTest 0.343 0.719 0.718 1
/proc/Age2 0.314 0.330 0.344 100000
/proc/Age1 0.062 0.078 0.123 100000


Your version isn't faster :P. In fact it is 5 times slower. Whoever told you block statements are slow is completely full of it. They are dirt cheap to do unlike division/multiplication and function calls which are much slower than addition, subtraction, comparisons, and branching.
In response to Theodis
Theodis wrote:
Your version isn't faster :P. In fact it is 5 times slower. Whoever told you block statements are slow is completely full of it. They are dirt cheap to do unlike division/multiplication and function calls which are much slower than addition, subtraction, comparisons, and branching.

That's one of the most useful things I've read in a while. Seriously.

Prodigal Squirrel
In response to Theodis
>                       Self CPU Time   Total CPU Time  Real Time   Calls
> /mob/verb/StartTest 0.343 0.719 0.718 1
> /proc/Age2 0.314 0.330 0.344 100000
> /proc/Age1 0.062 0.078 0.123 100000
>


Of course a point I failed to make was how moot it is to optimize this function anyway :P. This is only called once every second and the execution time of an individual call for either is in micro seconds.
In response to Nick231
This will cause the same problem as the original. By having a proc call itself (which will then call itself again, endlessly until it is either stopped for being an infinite loop, or it eats up all available memory and freezes (which will only happen if you set it to background or disable loop_checks, both of which are bad and you shouldn't have to do).

Hell Ramen's version isn't recursing so it isn't going to flood the stack. However he should spawn() the initial Aging call() as the proc never returns so any initialization code following the Aging() proc will never get executed.