Is a global string var better then smaller ones?
#1

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
pawn Код:
new str[128];
Please help me figure this out.
Reply
#2

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!
Reply
#3

Obviously reusing one large one would save memory but it would also cause problems if misused.
Reply
#4

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

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

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

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

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

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


Forum Jump:


Users browsing this thread: 1 Guest(s)