sscanf vs strcmp and strtok, whats faster?
#1

Ahoy friends

Currently im trying to avoid the use of strtok and strcmp so i try to use sscanf instead.

Is this way faster than using strcmp and strtok? because i still use the default if(foo) if(foo2) syntax.

My code:

Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
	new cmd[32], idx;
	sscanf(cmdtext,"s[32]d",cmd,idx);
        if(!sscanf(cmdtext[strlen("/freeze")+1], "u", target))	
	{
		if(IsPlayerAdmin(playerid))
		{
	
			
			//return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /freeze [playerid]");
			if(!IsPlayerConnected(target))
			return SendClientMessage(playerid, COLOR_WHITE, "SERVER: Player not connected");
			TogglePlayerControllable(target,false);
		}	
	}
	else
	{
		return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /freeze [playerid]");
		
	}
The old way was

Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
        new cmd[32], idx;
	cmd = strtok(cmdtext, idx);
        if(strcmp(cmd, "/freeze", true) == 0)
	{
		if(IsPlayerAdmin(playerid))
		{
			new targetplayer;
			if(sscanf(cmdtext[strlen("/freeze")+1], "u", targetplayer))
			return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /freeze [playerid]");
			if(!IsPlayerConnected(targetplayer))
			return SendClientMessage(playerid, COLOR_WHITE, "SERVER: Player not connected");
			TogglePlayerControllable(targetplayer,false);
		}	
	}
Any way to measure the performance , memory usage and speed?
Reply
#2

This might help in some way?


https://sampforum.blast.hk/showthread.php?tid=608474
Reply
#3

https://sampforum.blast.hk/showthread.php?tid=218491
Reply
#4

Holy cow thanks
I found out the way by using sscanf instead of strtok is a lot of times faster!!!

Result:

Bench for strcmp: executes, by average, 678.47 times/ms.
Bench for sscanf: executes, by average, 2612.83 times/ms.

I hope my way of checking it was correct

Код:
#include <a_samp>
#include <sscanf2>
#pragma tabsize 0
#define START_BENCH(%0); {new __a=%0,__b=0,__c,__d=GetTickCount(),__e=1;do{}\
		while(__d==GetTickCount());__c=GetTickCount();__d=__c;while(__c-__d<__a||\
		__e){if(__e){if(__c-__d>=__a){__e=0;__c=GetTickCount();do{}while(__c==\
					GetTickCount());__c=GetTickCount();__d=__c;__b=0;}}{
			
			#define FINISH_BENCH(%0); }__b++;__c=GetTickCount();}printf(" Bench for "\
		%0": executes, by average, %.2f times/ms.",floatdiv(__b,__a));}
new cmdtext[32];


strtok(const string[], &index)
{
	new length = strlen(string);
	while ((index < length) && (string[index] <= ' '))
	{
		index++;
	}

	new offset = index;
	new result[20];
	while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
	{
		result[index - offset] = string[index];
		index++;
	}
	result[index - offset] = EOS;
	return result;
}
stock strtok2(const string[], &index)
{
	new result[20], length = strlen(string), i = index;
	while ((i < length) && (string[i] == ' ')) i++;
	strmid(result,string,i,((index = strfind(string, " ", false, i)) == -1) ? (index = length) : (index) , 20);
	index++;
	return result;
}
main()
{
	
	{
		START_BENCH( 1000 );
		new cmd[32], idx;
		cmd = strtok(cmdtext, idx);
		format(cmdtext,sizeof(cmdtext),"/freeze 0");
		idx =0;
		if(strcmp(cmd, "/freeze", true) == 0)
		{
			
			idx =1;
		}
		FINISH_BENCH( "strcmp" );	
	}
	{
		START_BENCH( 1000 );
		new cmd[32], idx;
		format(cmdtext,sizeof(cmdtext),"/freeze 0");
		idx =0;
		sscanf(cmdtext,"s[32]d",cmd,idx);
		new target;
		if(!sscanf(cmdtext[strlen("/freeze")+1], "u", target))	
		{
			idx=1;
		}
		FINISH_BENCH( "sscanf" );
	}
	
	
}
Is there a way to compare my version with command processors like zcmd?
Reply
#5

strcmp - string comparator
sscanf - string scan format
strtok - string tokens (string splitting)

Every single one of them has its use.
While strtok can be replaced by explode or even (s)scanf it still has its use.

I guess what you are asking is what would be faster for command and command arguments processor. Well here is the catch, it doesnt metter witch one is faster, commands are not called that often to cause bottleneck in a code, there is no need to over optimize part of code that wont have impact on overall server performance.

Just use what you need in a given case, for processing command arguments. In samp sscanf is usually best way to go, and sometimes you would use it in combination with strcmp (for example you want to set player color, you will get string using sscanf, but compare it to colors array using strcmp).
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)