ID:2162959
 
So procs like this are complete trash and ruin cpu.
Is there a more efficient way of looping a check like this?
Maybe using built in procs.
while(loc)
for(var/M in view(1))
Whatever()
sleep(1)




var/m
for()
if(condition){break}
for(m in view(1))
...
sleep(1)
Mav472 wrote:
So procs like this are complete trash and ruin cpu.
Is there a more efficient way of looping a check like this?
Maybe using built in procs.
while(loc)
for(var/M in view(1))
Whatever()
sleep(1)

Could you give a more specific example, or some context?

There's nothing particularly inefficient about the snippet you've shown.

If Whatever() is inefficient, then the code that calls it might appear inefficient when it actually isn't. Or, you're running too many instances of this loop at the same time.
In response to Kozuma3
Seems like there isnt too much of a differences and this would continue to eat cpu, but i'll try it thanks.
In response to Kaiochao
var/mob/M
var/damage=1000


mob/proc/CanTakeDamage()
if(dead||sleeping)return 0
return 1


while(loc)
for(M in view(4,src))//src is an object
if(M.CanTakeDamage()&&M!=owner)
spawn()M.current_stamina-=damage
sleep(1)
In response to Mav472
A few tiny optimizations:

1. Get rid of the spawn(). Why is it even there?

2. Instead of checking if M != owner for every M, you could instead make sure that you don't include the owner in the list you're looping through:
for(M in view(4, src) - owner)
if(M.CanTakeDamage())


3. In CanTakeDamage(), instead of using an if(), you could instead return the equivalent expression:
// "a mob can take damage when neither dead nor sleeping"
mob/proc/CanTakeDamage()
return !(dead || sleeping)

You could also use a bit field to compress up to 16 Boolean variables into a single value.

If this code is still using a lot of CPU time, you're just gonna have to run it less often. You could increase the sleep delay, or you could just not have as many instances of the loop running. If you want more help there, you need to show more code; especially the parts that start this loop.

You've been extremely conservative in showing your code.
You don't need to hide your code.
We all know it's not worth stealing.
In response to Kaiochao
lmfao. Hey I was just speaking in general. I thought that the spawn() was required so that it only happens once. Thanks for the help though i'll try to incorporate your optimizations. Also thanks for explaining.
In response to Mav472
That's not even close to what spawn is about.