Old 14-03-2008, 05:10   #1 (permalink)
haku
shiro
 
haku's Avatar
 
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:
$a = // something that requires a query to the database $b = time(); $c = $_SERVER['REMOTE_ADDR'];

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:
$a = $a stuff; $b = $b stuff; $c = $c stuff; if(something) { do something with $a $b and $c } else { do something else } if(another thing) { do something } else { do something with $a $b and $c } if(one last thing) { do something with $a $b and $c } else { do something else }


B) Declare the variables in the places they are to be used, then use them
Code:
if(something) { $a = $a stuff; $b = $b stuff; $c = $c stuff; do something with $a $b and $c } else { do something else } if(another thing) { do something } else { $a = $a stuff; $b = $b stuff; $c = $c stuff; do something with $a $b and $c } if(one last thing) { $a = $a stuff; $b = $b stuff; $c = $c stuff; do something with $a $b and $c } else { do something else }

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.
  Reply With Quote
Old 14-03-2008, 05:47   #2 (permalink)
MikeMackay
Everything is fine.
 
MikeMackay's Avatar
 
Join Date: Feb 2005
Location: Farringdon, London
Posts: 640
Send a message via MSN to MikeMackay Send a message via Skype™ to MikeMackay
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
  Reply With Quote
Old 14-03-2008, 06:01   #3 (permalink)
haku
shiro
 
haku's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 1,306
Thats what Im thinking too. Which in retrospect probably means my example wasn't very good. I probably should have not made one of the variables be a query to the database.
  Reply With Quote
Old 15-03-2008, 10:49   #4 (permalink)
Hunch
Grumpy old man
 
Hunch's Avatar
 
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.
  Reply With Quote
Old 17-03-2008, 13:36   #5 (permalink)
Cheeto
Mhmmmm Cheesy!
 
Cheeto's Avatar
 
Join Date: Mar 2008
Location: Raytown, Missouri
Posts: 29
Send a message via AIM to Cheeto
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...
  Reply With Quote
Old 26-03-2008, 15:56   #6 (permalink)
dudefromthenet
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.
  Reply With Quote
Old 10-04-2008, 15:06   #7 (permalink)
jeffmmar
Registered User
 
Join Date: Feb 2006
Posts: 14
This is dumbest thing i've ever seen.
  Reply With Quote
Old 10-04-2008, 19:46   #8 (permalink)
haku
shiro
 
haku's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 1,306
Then shut the fuck up and ignore it.
  Reply With Quote
Old 18-04-2008, 18:10   #9 (permalink)
Naatan
Web Developer
 
Naatan's Avatar
 
Join Date: Jan 2007
Location: Brussels
Posts: 214
Send a message via MSN to Naatan
In my opinion; if you're gonna repeat the exact same piece of code more than twice.. put it into a function..
Code:
function getStuff() { $string = array(); $string['a'] = $a stuff; $string['b'] = $b stuff; $string['c'] = $c stuff; return $string; } if(something) { $str = getStuff(); do something with $str['a'] $str['b'] and $str['c'] } else { do something else } if(another thing) { do something } else { $str = getStuff(); do something with $str['a'] $str['b'] and $str['c'] } if(one last thing) { $str = getStuff(); do something with $str['a'] $str['b'] and $str['c'] } else { do something else }

"Problem" solved
__________________
Developer of Divia CMS,
More info? Visit my blog.
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8