/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] IF conditions… which is more efficient?

Hi all,

I need to be able to compare a lot of variables with another lot of variables.

Example:

[CODE]if ($varA1 == $varB1 &&
$varA2 == $varB2 &&
$varA3 == $varB3 &&


$varA46 == $varB46 &&
$varA47 == $varB47 &&
$varA48 == $varB48) {

//do something

}[/CODE]

[I]Note: There is a loop to generate the numbered variables above, but that’s not the issue. I just listed it “manually” to make ti clearer.[/I]

My query is this…
What is more efficient (in speed or memory or both)?
1) Comparing two sets of 48 variables as above?; or
2) Storing all of the variables above into two different strings and then doing a single comparison on the two strings, eg: if ($Avarsjoined == $Bvarsjoined) ?

Both methods work in my case, just wondering which is best to use.

Cheers,

Chris

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiJan 05.2010 — I would personally look to store the data as two arrays then compare the array.
Copy linkTweet thisAlerts:
@Chris_JacksauthorJan 05.2010 — Would that be easier on the processing power? (says I who knows little of such things)

Thanks once again MindZai!
Copy linkTweet thisAlerts:
@MindzaiJan 05.2010 — There will be no practical difference in terms processing. The difference is that it would make for much neater and cleaner code. Perhaps if you post the code which is currently generating variables I can give you an example.
Copy linkTweet thisAlerts:
@NogDogJan 05.2010 — While I would prefer to use arrays, you could clean it up a bit by using variable variables.
[code=php]
$flag = true;
foreach(range(1,48) as $i)
{
if(${'varA'.$i} != ${'varB'.$i}
{
$flag = false;
break;
}
}
if($flag)
{
// do something
}
[/code]
Copy linkTweet thisAlerts:
@Chris_JacksauthorJan 05.2010 — Thanks NogDog.

I'm actually using variable variables (with while loop) in my real chunk of code. I just "spelled it out" in it's long form to better demonstrate.

I was wondering which (if any) was more efficient at the technical/processing level...


joining all of these variables together as two big strings and then comparing each in the IF statement (a single comparison)....

...or leaving them as there individially stored selves and making 48 comparisons in the IF statement.
Copy linkTweet thisAlerts:
@MindzaiJan 06.2010 — I would guess using arrays would be quicker than both of those, but the real question is, "who cares?" :p We are talking tens of thousandths of a second - just use the method that reads nicest.
Copy linkTweet thisAlerts:
@Chris_JacksauthorJan 06.2010 — Ah excellent, if that's the case then indeed, who cares?! Cheers Mindzai
×

Success!

Help @Chris_Jacks spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...