| Home | Register | FAQ | Members List | Search | Today's Posts | Mark Forums Read |
|
|
#1 (permalink) |
|
shiro
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 1,306
|
Programming Theory
I have a question efficient script structures. Lets say I have variables $a, $b and $c. I don't know if it matters what they contain, but just in case, lets say that these are set with the following functions: Code:
Now in my script, I have various if/else statements. These three variables will be used in some of the conditionals, but not all of them. So what I am wondering is which of the two following structures would be better. A) declare the variables at the top, use them in the applicable places: Code:
B) Declare the variables in the places they are to be used, then use them Code:
Option A uses less code, as the variables are declared only once. However, they are declared whether or not they are necessary. Option B uses more code, but the variables are only declared in the event that they are needed. So which one of these do you think (or know) is more efficient, and why? note: I'm kind of suspecting option B, as I think that servers can probably process more code in a script faster than they can process unnecessary queries to the database. |
|
|
|
|
|
#2 (permalink) |
|
Everything is fine.
|
I guess it would depend if you need to access those variables globally or just locally to the function that requires them ? Depending on the situation you could always do both ? Store the variables you know need global access at the top before your conditionals, and then store the local ones inside the function. Perhaps someone with a better insight will step in and clarify it for you. As a side note, I wouldn't run any queries to the DB that weren't entirely necessary. I should imagine it's much better to invoke them on demand. - Mike Mike Mackay - Developer
|
|
|
|
#4 (permalink) |
|
Grumpy old man
Join Date: Oct 2007
Location: North Japan
Posts: 1,128
|
Declaring variables as and when they are needed is more efficient, however these kinds of problems generally aren't as much of an issue these days. Back when I was first started programming, machines had between about 1 to 8K of system memory, which meant storage was pretty important, and processors were so slow that any little hacks or tweaks could make a big difference to your codes performance. Even using short variable names instead of long words, made a difference. Nowadays with multi-gigahertz processors and gigabytes of RAM it's more important to make your code readable, modular and easy to maintain. Performance is only really an issue if you are writing games, or other calculation intensive applications. |
|
|
|
#5 (permalink) |
|
Mhmmmm Cheesy!
|
Agreed with Hunch on this one... readability of the code is foremost in today's applications. If you believe in the design standard of declaring your variables separate then your logic... then do it... Personally I try and declare my variables at the top of the script... but that's just how I've been programming for years... |
|
|
|
#6 (permalink) |
|
Registered User
Join Date: Mar 2008
Posts: 19
|
You should always remember that PHP is an interpreter (correct me if I am wrong). Perhaps it is a bit slower if you initialize variables and then do not use them, but not very much slower (nanoseconds). You should use the technique you like more/makes more sense. If it was a compiled language it would not matter, because the compiler optimizes the code anyway. |
|
|
|
#9 (permalink) |
|
Web Developer
|
In my opinion; if you're gonna repeat the exact same piece of code more than twice.. put it into a function.. Code:
"Problem" solved |
|
![]() |