You are invited to Log in or Register a free Frihost Account!

defined Constants against variable against arrays?

Consider a database configuration file like this one below

define ("DBNAME", "my_db");


$DBNAME = "my_db";


$setings['DBNAME'] = "my_db";

Which approach do you think is the best approach if performance is the criteria?
Agent ME
I don't really think that's something to need to worry much about performance-wise - It would probably make a bigger difference if you made sure to use variable names that had as few characters as possible so that the parser could read them faster (I'm not suggesting you do that as your code will become hard-to-read, just giving an example to show it's not worth worrying over unless you're going to have a system with a very heavy load, and then you probably would want to use compiled code instead of PHP if you did).
Theoretically, the second form, declaring it as a simple variable, could save you a couple of nanoseconds now and then. The important fact is that the database routines themselves will not be affected by the choice, so the same would be true for any use of a variable.

I agree with agent me on this one. In general, it is rarely worthwhile to write code for fastest performance. If you write code that is easy to understand and follow generally accepted good coding practices, you will almost always get adequate performance.
This is a bit of a trivial question since they will all complete in constant time. However, the third is likely the "slowest" since it requires you to look up the array address, then add the index.

However, this is only a single instruction increase on your processor. I don't think you're going to notice the extra millionth of a second compared to things which are orders of magnitude slower like db connections, network speed and your browsers rendering time!!!
Well, the second choice is somewhat the best.
But not as to memory or execution performance; it's because of reusability...
If you use multiple db connections and it's not a must to use them simultaneous, defining a constant for each one of them takes memory. Execution speed is almost the same (minor differences);
So, in this case, reusing the var can spare some memory; though it's not much.
Stubru Freak
I would say the first is better because it's easier for the compiler to know that DBNAME won't be changed again, so it can optimize the script by replacing DBNAME with my_db everywhere it encounters it.
I think PHP is dynamically compiled, but I'm not sure.
I don't know a lot about the internal working of PHP but that seems logical.
After a load of testing i discovered the athe first one is better and the last one is significantly bad
Stubru Freak
Can you give the results? Or isn't that possible?
Related topics
How To : Secure Your PHP Website
Tutorial: PHP Installed Modules Dynamic Reference Tool
Fashionable Nonsense by Alan Sokal and Jean Bricmont
Fortran Tutorials(77)
writing COM components in Perl
phpbb cash mod problemos
Multidimensional Arrays & passing info between windows
assembly compiler problem
Deism vs Atheism
Problems with php + mysql script.
Please help with &id tags!
PHP Remove Text?
The ethics of firing someone for off-the-job bad behaviour
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

© 2005-2011 Frihost, forums powered by phpBB.