ID:828367
 
Code:
verb/stress_test(n as num)
var/t=world.timeofday
for(var/i=1 to n)for(var/mob/combat/npc/m in oview(client.view))break
world<<"Loop Method::[n] checks took [world.timeofday-t] ticks"
t=world.timeofday
for(var/i=1 to n)locate(/mob/combat/npc) in oview(client.view)
world<<"Locate Method::[n] checks took [world.timeofday-t] ticks"


Problem description:
Would this be the way to check which is faster? Because by this test locate is slower than a for loop.
That sounds about right. I think locate() is more intensive than running a for loop in this case.

oview() generates a list of all objects within a set range.

If you run a for loop on that list and specify a type path within the for loop any elements in the list that do not match the type path will be internally ignored. Because you're immediately breaking out of the loop on the first element you find it would be finished pretty much instantly.

Running locate() on the list may be slower in this case because AFAIK the way it loops through objects internally is handled differently than the for loop. After all, locate() also allows you to search for a particular object given a tag.

There may still be ways to perfect your testing: locate() does accept a second parameter telling it to limit its search to a given list. Your method may be the equivalent of using that second parameter but I'm not sure.


What exactly are you trying to do? If you're trying to find a mob within range to attack then I recommend using a for loop as it will allow you to perform in-depth checks on the candidate.

You will also want to avoid constantly using at least the view/oview/viewers/oviewers/hearers/ohearers procs as those require additional calculations to determine if an object should belong in those lists. Instead you could use range/orange instead and check if the target can be seen after confirming (through simpler checks) that the target is a viable one.