Is a global string var better then smaller ones? -
Toni - 30.09.2010
Hello there, I was just curious about this one simple question I can't seem to figure out. I was discussing with Calgon about what would be better (in this case for code optimization). What I did was in my script, I created a large variable
only for
strings.
pawn Код:
new str[1024]; // This is also used for dialogs just saying (I know max is 128 for normal strings).
That is a global variable, that I put somewhere above/near the #include/#defines.
Now, that is all I use for string variables 1 var, globally. And I can't tell if declaring smaller string variables are better then global ones.
Imagine having to create small variables under every callback/function you use
Please help me figure this out.
Re: Is a global string var better then smaller ones? -
Scenario - 30.09.2010
I was told by somebody, who was told by a friend that it
is better to have
global variables. Although, I never really did any type of testing, etc...so I wouldn't take my word for it!
Re: Is a global string var better then smaller ones? -
Norn - 30.09.2010
Obviously reusing one large one would save memory but it would also cause problems if misused.
Re: Is a global string var better then smaller ones? -
Toni - 30.09.2010
Quote:
Originally Posted by Norn
Obviously reusing one large one would save memory but it would also cause problems if misused.
|
Would misusing it count as not clearing the variable?
Re: Is a global string var better then smaller ones? -
bigcomfycouch - 30.09.2010
I'd assume using a global string wouldn't be terrible. PAWN is single-threaded, so two things can't happen at the exact same time. Therefore, one variable can't be in use in two places simultaneously.
Correct me if I'm wrong.
Re: Is a global string var better then smaller ones? -
nemesis- - 30.09.2010
Quote:
Originally Posted by Norn
Obviously reusing one large one would save memory but it would also cause problems if misused.
|
Correct. I use 5 global strings spread throughout a 23K line gamemode. No problems here.
Quote:
Originally Posted by The Toni
Would misusing it count as not clearing the variable?
|
No. Misusing it would mean setting in, passing to a function that overwrites it, then back to the main function where it gets used again.
Quote:
Originally Posted by bigcomfycouch
I'd assume using a global string wouldn't be terrible. PAWN is single-threaded, so two things can't happen at the exact same time. Therefore, one variable can't be in use in two places simultaneously.
Correct me if I'm wrong.
|
Most people here preach using global strings as a tool of the devil. They usually do that because they're parroting what someone else said, because they have no clue what they're doing, or because they don't even run a server to begin with.
PAWN is single-threaded though plugins with multi-threaded features (i.e. G-Stylez MySQL) do exist. There you need to be careful.
Re: Is a global string var better then smaller ones? -
RoBo - 01.10.2010
Quote:
Originally Posted by Norn
Obviously reusing one large one would save memory <snip>
|
Would it really?
With a global var the memory is always allocated, so no matter what PAWN is executing that string will be taking up memory. If it were in a function, only when that function is executing would the memory be used. PAWN only allocates enough memory for the biggest function + global vars, so you could potentially use more memory with a single global var.
Quote:
Originally Posted by bigcomfycouch
I'd assume using a global string wouldn't be terrible. PAWN is single-threaded, so two things can't happen at the exact same time. Therefore, one variable can't be in use in two places simultaneously.
|
It's a bad coding practice, might as well scrap functions in return for goto while you're at it.
Re: Is a global string var better then smaller ones? -
nemesis- - 03.10.2010
Quote:
Originally Posted by RoBo
Would it really?
With a global var the memory is always allocated, so no matter what PAWN is executing that string will be taking up memory. If it were in a function, only when that function is executing would the memory be used. PAWN only allocates enough memory for the biggest function + global vars, so you could potentially use more memory with a single global var.
|
Yes.. lets conserve a menial amount of memory. 4gb of it gets used up so quick, right?
Re: Is a global string var better then smaller ones? -
RoBo - 03.10.2010
Quote:
Originally Posted by nemesis-
Yes.. lets conserve a menial amount of memory. 4gb of it gets used up so quick, right?
|
What? All I said is that using global vars may use more memory than local ones. Conserving memory is a good thing, you'd have to be a moron to think otherwise, but I'll leave you be in your fantasy world of PAWN being fine with 40mb of vars, let alone 4gb.