By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,173 Members | 1,079 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,173 IT Pros & Developers. It's quick & easy.

Which is faster? While vs For, if vs if...else

P: n/a
Hi

I have a costum function for a special search, which sort strings.

This is currently the place where I can save a lot of time (~70%) if
possible.

So, which is faster:

for($j = 0;$j<count($array);$j++)
or

$j = 0
while($j<count($array))
$j++;

(well, I know that counting down is faster, so...

$j = count($array)-1;
while($j>=0)
$j--;
)

ALSO

Which is faster:

if .... else ...
or:
if (true) ...
if (false) ...

BR
Sonnich

Oct 18 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Sonnich wrote:
Hi

I have a costum function for a special search, which sort strings.

This is currently the place where I can save a lot of time (~70%) if
possible.

So, which is faster:

for($j = 0;$j<count($array);$j++)
or

$j = 0
while($j<count($array))
$j++;

(well, I know that counting down is faster, so...

$j = count($array)-1;
while($j>=0)
$j--;
)

ALSO

Which is faster:

if .... else ...
or:
if (true) ...
if (false) ...

BR
Sonnich
I don't know any specifics on PHP's implementation of such things, so I
can't comment on that.

I can say, however, that if you need to focus on such trivial things in
order to improve performance, then maybe it's your algorithm that could
benefit from the attention instead.

Have you calculated the O (big-O) for the algorithm? Reducing it by at
least an order of magnitude would improve performance well over
whatever gain you may be able to squeeze out by changing a while to a
for.

You say you're sorting strings? Are you using a well-known sorting
algorithm, or something homegrown? Have you tried QuickSort?
MergeSort? HeapSort? InsertionSort (only good for sorting small
collections)?

QuickSort is about the cream of the crop, last I knew, though, so if
you're already using it, maybe you actually do need to look into the
performance differences between the control structures.

Moot

Oct 18 '06 #2

P: n/a
Also sprach Sonnich:
So, which is faster:

for($j = 0;$j<count($array);$j++)
or

$j = 0
while($j<count($array))
$j++;
I don't think they make much difference. If the "direction" doesn't matter,
the following is much (!) faster as count($array) is calculated only once
and there's one expression less to evaluate:

for ( $i = count( $array ); $i--; )
{
// do useful stuff
}

OTOH, if the "useful stuff" takes a lot of time, the performance gain might
turn out to be negligible in comparison.
Which is faster:

if .... else ...
or:
if (true) ...
if (false) ...
I think, "if...else" should be slightly faster as only one condition needs
to be evaluated.

Greetings,
Thomas
Oct 18 '06 #3

P: n/a
Sonnich wrote:
Hi

I have a costum function for a special search, which sort strings.

This is currently the place where I can save a lot of time (~70%) if
possible.

So, which is faster:

for($j = 0;$j<count($array);$j++)
or

$j = 0
while($j<count($array))
$j++;

(well, I know that counting down is faster, so...

$j = count($array)-1;
while($j>=0)
$j--;
)
They're both slow. foreach() is the fastest.

Oct 18 '06 #4

P: n/a
Rik
Chung Leong wrote:
Sonnich wrote:
>So, which is faster:

for($j = 0;$j<count($array);$j++)
or

$j = 0
while($j<count($array))
$j++;

(well, I know that counting down is faster, so...

$j = count($array)-1;
while($j>=0)
$j--;
)

They're both slow. foreach() is the fastest.
Very true indeed.

Then again, the looping is often not the problem, more the actual logic
that is performed within the loop. If you need looping, you need looping,
no denying that. The logic inside the loop however, often has very useless
duplicate assigments etc. When looping, be sure to set everything not
related to the loop outside that particular loop.

That seems very obvious, but I cannot count the times I've seen loops with
identical assignments over and over again.
--
Rik Wasmus
Oct 19 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.