which code is more efficient?

stndn

Golden Member
Mar 10, 2001
1,886
0
0
Suppose i have script that calls up a configuration for name of months.
Which one i'll use depends on what script is calling the configuration file.

So, assume that i have these in my configuration file:
$regMonth = array ("Jan", "Feb", "Mar", "Dec");
$crazyMonth = array ("ABC", "DEF", "GHI", "JLK");
$numericMonth = array ("01", "02", "03", "12");
$otherMonth = array ("j4n", "f3b", "m4r", "d3c");

Would it be more efficient if i change the above to:
if ($arg1 == "regular") {
$regMonth = array ("Jan", "Feb", "Mar", "Dec");
}

if ($arg2 == "crazy") {
$crazyMonth = array ("ABC", "DEF", "GHI", "JLK");
}

if ($arg3 == "numeric") {
$numericMonth = array ("01", "02", "03", "12");
}

if ($arg4 == "other") {
$otherMonth = array ("j4n", "f3b", "m4r", "d3c");
}

In my configuration file, there are more than 4 arrays defined, but i'll probably use only about 2-3 of them for a script that i'm executing.

So, my question is:
Will it be more efficient/effective/etc to just define all the arrays regardless of whether i'll use them, or is it better to have a bunch of if() statements that will create the arrays only as necessary?

Thanks.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Well first of all, conditionally creating a variable like that is nasty IMO.

I'd imagine that the second is more efficient. Basically, to check string equality, it'll just check each character for equality. With fairly short strings like that, that's pretty trivial. But creating new arrays of strings will mean creating a few new arrays, and a bunch of new strings. I don't know how PHP manages memory, but creating all of those arrays of strings might be a good deal more intensive than doing checks and then not creating them.

But either way, unless there's a LOT of these, it probably won't make much difference either way. Time it and see.
 

Mannkind

Senior member
Mar 19, 2000
648
0
76
I'm no expert with O notation, but I would think either code would produce a constant O. No matter the actual value, when you lump it in with anything else, the actual value of the constant is moot.

Anyone care to backup/reject my thoughts? Either way, I'm interested to know.
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
if i were given 2 extra days to work on this project, i'd sit down and time the differences and think of a way to make them more efficient. ..
unfortunately, i don't -(

and honestly, i initially did those array declarations simply because i was too lazy to create multiple separate files that has only array declarations, and call the file according to whichever or whatever variable(s) i need to use.

i *could* move them out of the configuration files and insert them directly to the various scripts that need them.. that way i don't have to think of which one is more efficient between those two, or if there are ways to make them more efficient..

but i just don't feel like changing them now. especially not on a monday afternoon ,p

still, i agree that declaring tons of variables that way is just downright nasty unless they are needed by every scripts i wrote.
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
by the way .. would it make a lot more differences if those variables i declare are plain strings, instead of array of string?
ie:
$regMonth = "Jan";
$crazyMonth = "ABC";
$numericMonth = "01";
$otherMonth = "j4n";
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: stndn
by the way .. would it make a lot more differences if those variables i declare are plain strings, instead of array of string?
ie:
$regMonth = "Jan";
$crazyMonth = "ABC";
$numericMonth = "01";
$otherMonth = "j4n";

Well, instead of making n*4 strings you are making 1*4 strings. Sound more efficient?
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
Hmmm... you're right.
1 string is better than 4 strings, definitely....

on a similar note, then...
if i define function in an include file, but only use the function about 2 out of 20 times ...
then it's better for me to put the function in a new file, and only include them as necessary?
instead of putting all the different functions in one file that gets included and only use a handful of them at a time?

thanks again...

/me has been enlightened -)
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
It'd be most efficient to try and put different functionality in different files, and only include what you need.

What are you doing that needs to be so efficient anyways?
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
Okay, thanks for the information.

I'm doing this for my work.
Basically what happened was the previous guy included a whole bunch of functions and variable declarations and array declarations in an "include.php" file, and that file is being read with each PHP script execution.

I've been thinking of taking that "include.php" file apart, split them according to what's needed and what's not, and try to make the scripts run more efficiently.

It's not really something that i have to do .. but i thought, "hey, i have the skills, why not do it? i'll just have to wait until i have spare time, though"...

On a side note .. having worked with this mess and asked these questions also gave me some new insights on how to deal with "efficient code reuse" and "non-reusable code efficiency" ...
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |