Forum Moderators: coopster

Message Too Old, No Replies

Multiple OR instances

         

havoc

5:53 am on Apr 30, 2003 (gmt 0)

10+ Year Member



I dont seem to remember how to use multiple OR statements .. i know you cant have more than one set but i have forgotten how to check a verible for "matching strings".

For EG

if currentcond = "sunny"

if ( $currentcond == "Possible shower" ¦¦ "Showers easing" ¦¦ "Showers increasing")
{
$conditions = "<img src=\"images/possible_shower.gif\" width=\"53\" height=\"39\"><br>$currentcond";
}

it would still show the showers icon..

i know this is wrong but i cant find anywhere that shows how to do it correctly

jatar_k

5:56 am on Apr 30, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if ($currentcond == "Possible shower" ¦¦ $currentcond == "Showers easing" ¦¦ $currentcond == "Showers increasing")

remember to replace broken pipes with proper ones

havoc

6:07 am on Apr 30, 2003 (gmt 0)

10+ Year Member



This works .. thanks .. i though there was another way as well

grahamstewart

10:23 am on Apr 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or better still, use the switch statement. This keeps it readable and makes it a lot easier to expand when you add other weather conditions.

[pre]
switch( $currentcond ) {
case 'sunny':
print '<img src="images/sun.gif"><br />'.$currentcond;
break;

case 'Possible shower':
case 'Showers easing':
case 'Showers increasing':
print '<img src="images/showers.gif"><br />'.$currentcond;
break;

default:
print '<img src="images/question.gif"><br />Who Knows?';
}
[/pre]

grahamstewart

10:34 am on Apr 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Alternatively since you always want to do more or less the same thing but with a different image, you could define an array that maps condition to image and then use that.
Like this..

[pre]
$conditionToPic = array(
'sunny' => 'sunny.gif',
'Possible shower' => 'shower.gif',
'Showers easing' => 'shower.gif',
'Showers increasing' => 'heavyrain.gif',
'Snow' => 'snowflake.gif' );

if ( isset($conditionToPic[$currentCond]) )
print '<img src="'.$conditionToPic[$currentcond].'"><br />'.$currentcond;
[/pre]

That way you can move the actual mapping to a config.php file so you can find it easier at a later date.

daisho

2:28 pm on Apr 30, 2003 (gmt 0)

10+ Year Member



grahamstewart both your solutions are nicer and have their benifites but jatar_k's solution would be the fastest for parsing and executing.

PHP's IF statement will quite as soon as it's sure of an answer.

ie with:
if ( $currentcond == "Possible shower" ¦¦ "Showers easing" ¦¦ "Showers increasing")

if $currentcond == "Possible shower" is true then the statement does not evaluate the rest of the statement and jumps right to the code block because if knows that all the options are "OR'd" so that any TRUE is an overall TRUE.

grahamstewart I like your array solutions for this over the case it's a more natural fit but much more overhead compared to the 1 liner if statement.

daisho

grahamstewart

11:31 pm on Apr 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jatar_k's solution would be the fastest for parsing and executing.

Doubt it.

If anything the Switch statement should actually be (very marginally) faster than if, because you are explicity stating that you are doing all your compares against the same variable.

And the array solution will be faster still for the average case, since arrays use hashing.

PHP's IF statement will quit as soon as it's sure of an answer.

So will switch. And the array.

array solutions... much more overhead

Much more overhead? Not really.
Okay so I create a global array, but its not exactly a huge drain on resources. Couple of bytes will make no difference.

However, the point I'm trying to make in all this is:
Don't get too hung up on 'efficiency'. Especially at this level.

Often you will be making false assumptions anyway, and even if you do manage to save a couple of bytes of stack space or a few CPU clock cycles - the difference will be so infinitesimally tiny that you'd have to run the function a couple of million times to see it.

Instead write your code so that it is easy to understand and flexible. Something like 70% of the development cycle is spent in maintentance [htc.honeywell.com] so the easier it is to maintain the simpler your life will be.

daisho

12:05 am on May 1, 2003 (gmt 0)

10+ Year Member



I guess it mainly has to do with your style of coding that's what makes life great :).

Though I do always try to balance speed and readability, I agree that in this case the difference would take ages to notice, but at a point it still does make a difference. If you always say "What does it matter a few milliseconds here a few milliseconds their" they end up adding up. If you had a large system with 100 places that look a bit of extra time then had them in a loop all of a sudden you code is running 10s slower to generate a webpage.

Looking at each piece you'd never guess. That's why I always try to err on the side of speed.

daisho.

grahamstewart

1:04 am on May 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Trust me I've been there - trying to optimise every line of code. It can be part of the challenge of programming. But once you've worked on a few big projects you realise that for 95% of code, readability is way more important than speed.

For the other 5% that is within loops that get called often or have a very large number of iterations then speed can take more of a priority.

Out of interest I wrote this little script. It executes each of the three techniques mentioned one million times. For fairness sake, I made the currentcond 'Showers easing' which is the third option in each technique.

[pre][1]
<?php

function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}

$iter = 1000000;
$currentcond = 'Showers easing';

$starttime= getmicrotime();
for ( $x = 0; $x < $iter; $x++ ) {
if ( $currentcond == 'sunny' )
$result = '<img src="images/sun.gif"><br />'.$currentcond;

else if ( $currentcond == "Possible shower" ¦¦ $currentcond == "Showers easing" ¦¦ $currentcond == "Showers increasing")
{
$result = '<img src="images/sun.gif"><br />'.$currentcond;
}
}
$endtime = getmicrotime();
$time = $endtime - $starttime;
print 'If: '.$time." seconds\n";

$starttime = getmicrotime();
for ( $x = 0; $x < $iter; $x++ ) {
switch ( $currentcond ) {
case 'sunny':
$result = '<img src="images/sun.gif"><br />'.$currentcond;
break;

case 'Possible shower':
case 'Showers easing':
case 'Showers increasing':
$result = '<img src="images/showers.gif"><br />'.$currentcond;
break;

default:
$result = '<img src="images/question.gif"><br />Who Knows?';
}
}
$endtime = getmicrotime();
$time = $endtime - $starttime;
print 'Switch: '.$time." seconds\n";

$starttime= getmicrotime();
$conditionToPic = array(
'sunny' => 'sunny.gif',
'Possible shower' => 'shower.gif',
'Showers easing' => 'shower.gif',
'Showers increasing' => 'heavyrain.gif',
'Snow' => 'snowflake.gif' );

for ( $x = 0; $x < $iter; $x++ ) {
if ( isset($conditionToPic[$currentcond]) )
$result = '<img src="'.$conditionToPic[$currentcond].'"><br />'.$currentcond;
}
$endtime = getmicrotime();
$time = $endtime - $starttime;
print 'Array: '.$time." seconds\n";
?>
[/1][/pre]

The results on my box are:

If: 6.4893039465 seconds
Switch: 5.79472005367 seconds
Array: 5.01467204094 seconds

So in other words, for this case, the array method is faster by just over one millionth of a second.

Since this code is only run once per page it doesn't really matter which technique you use. So go with the one that you find readable and which suits your purpose best.

havoc

7:26 am on May 1, 2003 (gmt 0)

10+ Year Member



Very Interesting! im always up for some optimisation. I think switch is looking good :)

but i think people went to far to much effort but thanks :D

grahamstewart

7:38 am on May 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Doh... I wasn't trying to illustrate that one was particularly better than the other. Yes, 'switch' beats 'if' in this case, but then 'if' should win if the currentcond is 'sunny'. 'Array' should win on average and is more scalable.

The point I was trying to make is that your talking about a difference of around one millionth of a second - so for a once in a page operation like this it really doesn't matter a jot which you use.

In fact, unless it's something that gets executed thousands of times, it's just not worth worrying about.

Don't be swayed by false efficiency - 'faster' code is not always 'better' code. Use the code that suits your situation and is the most readily understandable and readable.