SA-MP Forums Archive
Some questions about strings and memory usage. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Some questions about strings and memory usage. (/showthread.php?tid=357619)



Some questions about strings and memory usage. - Vvolk - 07.07.2012

Hello eveybody! I have curiuos question. Which of 2 methods bellow of using strings uses less memory and gives better performance:

Код:
//First method. I think that this method is worse than second, because there are some string.
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
	switch(dialogid)
	{
    	case 477:
    	{
			if(response)
			{
			    new string[113],name[MAX_PLAYER_NAME];
			    GetPlayerName(playerid,name,sizeof(name));
			    format(string,sizeof(string),"Hello! Your name is %s. Your wanted level is %d stars. You have %d dollars.",name,GetPlayerWantedLevel(playerid),GetPlayerMoney(playerid));
			    SendClientMessage(playerid,-1,string);
			}
    	}
    	case 1025:
    	{
			if(response)
			{
			    new string[128],name[MAX_PLAYER_NAME];
			    GetPlayerName(playerid,name,sizeof(name));
			    format(string,sizeof(string),"Hello! Your name is %s. Your wanted level is %d stars. You have %d $. Have a nice day! :)",name,GetPlayerWantedLevel(playerid),GetPlayerMoney(playerid));
			    SendClientMessage(playerid,-1,string);
			}
    	}
	}
	return 0;
}


//==================================================================================


//Second method. I think that this method is better than first, because it uses only one string.
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
	new string[128],name[MAX_PLAYER_NAME];
	switch(dialogid)
	{
    	case 477:
    	{
			if(response)
			{
			    GetPlayerName(playerid,name,sizeof(name));
			    format(string,sizeof(string),"Hello! Your name is %s. Your wanted level is %d stars. You have %d dollars.",name,GetPlayerWantedLevel(playerid),GetPlayerMoney(playerid));
			    SendClientMessage(playerid,-1,string);
			}
    	}
    	case 1025:
    	{
			if(response)
			{
			    GetPlayerName(playerid,name,sizeof(name));
			    format(string,sizeof(string),"Hello! Your name is %s. Your wanted level is %d stars. You have %d $. Have a nice day! :)",name,GetPlayerWantedLevel(playerid),GetPlayerMoney(playerid));
			    SendClientMessage(playerid,-1,string);
			}
    	}
	}
	return 0;
}
Thank you for yours answers.


Re: Some questions about strings and memory usage. - coole210 - 07.07.2012

I would say the second method since your declaring the string once and using it for multiple purposes


Re: Some questions about strings and memory usage. - Vince - 07.07.2012

Personally, I opt for the second method, but it really doesn't matter. You're optimizing the wrong things.


Re: Some questions about strings and memory usage. - Vvolk - 07.07.2012

Quote:
Originally Posted by Vince
Посмотреть сообщение
Personally, I opt for the second method, but it really doesn't matter. You're optimizing the wrong things.
Then, what I need to optimize for better performance and less memory usage?


Re: Some questions about strings and memory usage. - coole210 - 07.07.2012

Quote:
Originally Posted by Vvolk
Посмотреть сообщение
Then, what I need to optimize for better performance and less memory usage?
Something small like that won't make a huge difference in better performance and less memory usage. You can take the easy way out and use JIT (Just-In-Time) compiler, there is a new 1.2 version on Zeex's github.


Re: Some questions about strings and memory usage. - Vvolk - 07.07.2012

Thank you very much. But is there any tutorial of using JIT? Because I can't understand what this thing does...


Re: Some questions about strings and memory usage. - coole210 - 08.07.2012

Quote:
Originally Posted by Vvolk
Посмотреть сообщение
Thank you very much. But is there any tutorial of using JIT? Because I can't understand what this thing does...
Nah, you just load the plugin and it works. There is a wikipedia page explaining what JIT compilers do though, include in the plugin's topic.


Re: Some questions about strings and memory usage. - SuperViper - 08.07.2012

The first one is better because you don't need 128 cells for each case, which is what you're doing in the second one. Just because code is in several places at once doesn't exactly mean it's less efficient. The difference between the two though is VERY slight and unless you want to be completely perfect, then I prefer the second one to save yourself the time of typing out the line.