1
\$\begingroup\$

Time is very important from times immemorial. Doing great things and making it happen in less time is more important as it can mean less effort. So I really like to know which way is faster and more efficient in the following codes, and I would happy to know any other way we can achieve the same code:

$month=date('m');
//Using If..else
 if($month==1){
    echo "January";
}elseif($month==2){
    echo "February";
}elseif($month==3){
    echo "March";
}elseif($month==4){
    echo "April";
}elseif($month==5){
    echo "May";
}elseif($month==6){
    echo "June";
}elseif($month==7){
    echo "July";
}elseif($month==8){
    echo "August";
}elseif($month==9){
    echo "September";
}elseif($month==10){
    echo "October";
}elseif($month==11){
    echo "November";
}else{
    echo "December";
}
//Using Switch
switch($month){
case 1:
    echo "January";
break;
case 2:
    echo "February";
break;
case 3:
    echo "March";
break;
case 4:
    echo "April";
break;
case 5:
    echo "May";
break;
case 6:
    echo "June";
break;
case 7:
    echo "July";
break;
case 8:
    echo "August";
break;
case 9:
    echo "September";
break;
case 10:
    echo "October";
break;
case 11:
    echo "November";
break;
default:
    echo "December";
break;
}
//Using Array
$month_s=array(1=>"January",2=>"February",3=>"March",4=>"April",5=>"May",6=>"June",
7=>"July",8=>"August",9=>"September",10=>"October",11=>"November",12=>"December");
echo isset($month_s[$month]) ? $month_s[$month] : '';
\$\endgroup\$
  • 5
    \$\begingroup\$ write a script, use $start = microtime(true) and then microtime(true) - $start to get a rough idea of the time each approach takes, and you have your answer. Do try with a larger array, too \$\endgroup\$ – Elias Van Ootegem Dec 1 '13 at 12:00
8
\$\begingroup\$

Like I said in my comment: If you want to know which is faster, write a benchmarking script, it's that easy:

$start = microtime(true);
//if-hell approach
$ifs = microtime(true) - $start;
$start = microtime(true);
//switch
$switch = microtime(true) - $start;
$start = microtime(true);
//array
$array = microtime(true) - $start;

Then compare and echo the results in any way you like.
My gut tells me that in this case, the switch will probably be the faster option. Considering a switch construction is sort of like labeling blocks of code, and then do some sort of eval-go-to thingy.
Branching (if) is faster in terms of compilation, perhaps, but for the month of december, you'll end up performing all if statements.
The array approach, for this example, doesn't look like the fastest approach you can take in this case, because it's constructing an array, performing a HashTable lookup, evaluate the result of that lookup, then perform the same lookup again, or return an empty string. HashTable lookups are as fast as it gets, of course, but still, you're having to do 2 of them, just after you've constructed the HashTable. The other approaches don't construct to then perform a lookup.

Mind you, This is micro-optimization of the worst kind. It's completely pointless and the speed difference won't be noticeable. Especially if you use some form of OP-Code caching (APC, for example). That should be the first port of call if you're trying to optimize some code.

What code should you use?:
This is the better question to ask. The efficiency of some snippet of code isn't just determined by the time it takes to execute. Efficient code is good code (is stable, performant, error and notice-free) and is easy to read, update and maintain.
If your determining the name of the month then it's pretty obvious the data-set is limited to 12 valid strings. But in most cases, you might end up with an ever growing switch or if-elseif-else tree. That's not efficient coding. The moment you have to scroll to see a single block of code, you can safely say it's time to think about refactoring your code.

That's why I'd either use the array approach (works just as well when data is coming from a query, so you can create a function that you can reuse). Or, if you are working on dates, I'd give way to normalization, and use the DateTime class. when I see new DateTime or $d = DateTime::createFromFormat, I know what to exepct. Even more so when I see:

[public] function doStuff(DateTime $date)
{
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ thank you very much. I used to think that good codes use less time and effort. The code I provided is just an example. So do I prefer Array. My experience in php is just 11 months old. Does it mean putting codes into functions or objects when you said about 'codes refactoring'? \$\endgroup\$ – Mawia HL Dec 1 '13 at 13:06
  • 1
    \$\begingroup\$ @MawiaHL: the term "refactoring code" can mean a variety of things: A new version of PHP is deployed and some extension (like mysql_*) are deprecated, the process of removing all calls to that extension is refactoring the code. If some intern wrote a script that doesn't match the coding standards you uphold, that script has to be normalized, in doing so, you tend to optimize some sections of that script, this, too, is refactoring. If you've got some code, and want to make it OO, then you'll have to refactor it, too, but in that case, I'd rather call it a rewrite :) \$\endgroup\$ – Elias Van Ootegem Dec 1 '13 at 13:49
  • \$\begingroup\$ A look up array OR a list would be the most efficient way of doing it. I'm tending towards the list more than array tbh though I think that's just personal habit \$\endgroup\$ – Dave Dec 1 '13 at 19:30
  • 1
    \$\begingroup\$ @Dave: A PHP array is what you'd call a list or vector in most other languages. Even so: the lookup will be fast, but you have to take the constructing of that list into account, too. Either way, I'd use an array for this, too: it's the more scalable/maintainable approach for something like this. When the choice is between an ever growing switch or an array, go with the A-team :) \$\endgroup\$ – Elias Van Ootegem Dec 1 '13 at 19:45
  • 2
    \$\begingroup\$ @EliasVanOotegem still puts you 90% of the way ahead of everyone else though :) I'm fairly new to php only really been using it 6 years or so. Coming from an out of order language though something that always makes me laugh is "oo php" \$\endgroup\$ – Dave Dec 2 '13 at 9:47
8
\$\begingroup\$

Neither will be simpler than using the date function's F code, and I'd bet $5* this would be faster than both.

$month = date('F');

For example,

> echo date('F');
November

* Coincidentally, $5 is the maximum amount of money you should spend trying to optimize this code. ;)

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.