Raised This Month: $51 Target: $400
 12% 

FindEntityByClassname vs looping


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Chaosxk
Veteran Member
Join Date: Aug 2010
Location: Westeros
Old 11-19-2016 , 17:29   FindEntityByClassname vs looping
Reply With Quote #1

Hey, i have to find several entities and kill them, wondering which one would be more efficient

This
Code:
void RemoveExistingBoss()
{
	int ent = -1;
	while ((ent = FindEntityByClassname(ent, "headless_hatman")) != -1)
		if (IsValidEntity(ent)) 
			AcceptEntityInput(ent, "Kill");
			
	while ((ent = FindEntityByClassname(ent, "eyeball_boss")) != -1)
		if (IsValidEntity(ent))
			AcceptEntityInput(ent, "Kill");
			
	while ((ent = FindEntityByClassname(ent, "merasmus")) != -1)
		if (IsValidEntity(ent))
			AcceptEntityInput(ent, "Kill");
			
	while ((ent = FindEntityByClassname(ent, "tf_zombie")) != -1)
		if (IsValidEntity(ent))
			AcceptEntityInput(ent, "Kill");
}
or this?
Code:
void RemoveExistingBoss()
{
	char classname[32];
	for (int i = MaxClients + 1; i <= 2048; i++)
	{
		if (!IsValidEntity(i))
			continue;
		
		GetEntityClassname(i, classname, 32);
		
		if (!strcmp(classname, "headless_hatman", false))
			AcceptEntityInput(i, "Kill");
		
		else if (!strcmp(classname, "eyeball_boss", false))
			AcceptEntityInput(i, "Kill");
			
		else if (!strcmp(classname, "merasmus", false))
			AcceptEntityInput(i, "Kill");
			
		else if (!strcmp(classname, "tf_zombie", false))
			AcceptEntityInput(i, "Kill");
	}
}
I'm leaning more towards the looping but wondering what others have to say.
__________________

Last edited by Chaosxk; 11-19-2016 at 17:31.
Chaosxk is offline
psychonic

BAFFLED
Join Date: May 2008
Old 11-19-2016 , 19:09   Re: FindEntityByClassname vs looping
Reply With Quote #2

It depends on how many entities, both networked and non-networked are in the server.

FindEntityByClassname already has a list of valid entities, so instead of also checking if an entity is valid, it only need to check the classname. It also runs in native code. However, it also looks at (valid) non-networked entities, which you stop before you hit if you are only going to 2048.

If you want to know which is faster for sure, time them.
psychonic is offline
Chaosxk
Veteran Member
Join Date: Aug 2010
Location: Westeros
Old 11-19-2016 , 21:48   Re: FindEntityByClassname vs looping
Reply With Quote #3

Well just timed both of them using GetGameTime() and the difference in time is 0, unless i did something wrong.

Code:
float a = GetGameTime();
PrintToChatAll("%f", a);
	
//code here

float b = GetGameTime();
PrintToChatAll("%f", b);
Quote:
Originally Posted by psychonic View Post
FindEntityByClassname already has a list of valid entities, so instead of also checking if an entity is valid, it only need to check the classname.
Good to know, never knew it was already valid.

I guess i will keep it with FindEntityClassname() just for the readability.
__________________

Last edited by Chaosxk; 11-19-2016 at 21:51.
Chaosxk is offline
Benoist3012
Veteran Member
Join Date: Mar 2014
Location: CWave::ForceFinish()
Old 11-20-2016 , 11:50   Re: FindEntityByClassname vs looping
Reply With Quote #4

Not sure if this is still a thing, but you should use the profiler function https://sm.alliedmods.net/new-api/profiler, to know how much time a part of your code takes to run.
__________________
Benoist3012 is offline
Reply



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

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

Forum Jump


All times are GMT -4. The time now is 10:50.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode