Some questions about strings and memory usage.
#1

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.
Reply
#2

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

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

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?
Reply
#5

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.
Reply
#6

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

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.
Reply
#8

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)