473,395 Members | 1,558 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
4 2635
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: david | last post by:
I write a program which returns the maximum of three input integers. both can run weil in the DEV-C++ , but I just wonder which one is better, and I also want to make it clear that if I use the...
29
by: guru.slt | last post by:
"the c++ standard library, tutorial and reference" book, it says: ++i is faster than i++. the latter involves a temporary object because it must return the old value/object of i. for this reason,...
1
by: Danny Dy | last post by:
Hi To All, I always write My VBA code in SQL(see Example). Private Sub cbxAEName_NotInList(NewData As String, Response As Integer) Dim stSQL as String Dim strMsg As String strMsg = "'" &...
12
by: junky_fellow | last post by:
Which is better using a switch statement or the if-then equivalent of switch ?
65
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
9
by: niteshs | last post by:
Hello all, just checking which method is faster string A; A = ""; if (A == string.Empty) { MessageBox.Show("a is empty"); } else
8
by: Scott Emick | last post by:
I am using the following to compute distances between two lat/long coordinates for a store locator - (VB .NET 2003) it seems to take a long time to iterate through like 100-150 locations -...
16
by: Brian Tkatch | last post by:
Is there a way to check the order in which SET INTEGRITY needs to be applied? This would be for a script with a dynamic list of TABLEs. B.
8
by: Sing | last post by:
Dear C Gurus, I would like to optimise a max() algo that my program uses many times. Which one is faster or are they the same? 1. #define max(a,b) (((a) (b)) ? (a) : (b)) 2. if (a>b) return a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.