473,396 Members | 1,724 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,396 software developers and data experts.

Is foreach the fastest way to do this?

I need 5 queries from the database, which I display in a php foreach
loop the following way.

$query1 = $DB->query("SELECT ... DESC LIMIT 20");
$query2 = $DB->query("SELECT ... DESC LIMIT 20");
$query3 = $DB->query("SELECT ... DESC LIMIT 30");
$query4 = $DB->query("SELECT ... DESC LIMIT 9");
$query5 = $DB->query("SELECT ... DESC LIMIT 9");

foreach ($query1->result as $row) {
echo '<a href="httplink/'.$row['topic_id'].'/"
title="'.htmlspecialchars(trim($row['screen_name'])).':
'.htmlspecialchars(trim($row['title'])).'">&raquo;
'.substr($row['title'], 0, 20)." ...</a><br />
";}
foreach ($query2->result as $row) {
echo '<a href="httplink/'.$row['topic_id'].'/"
title="'.htmlspecialchars(trim($row['screen_name'])).':
'.htmlspecialchars(trim($row['title'])).'">&raquo;
'.substr($row['title'], 0, 20)." ...</a><br />
";}

and so on....

Looking only at performance is there a quicker way to do this? Could I
use a different loop (other than foreach) to get better performance?

I found the following page, but I am not sure which one would help me
and how to change the code around to make it work. Any ideas would
help. Thank you!
http://www.php.lt/benchmark/phpbench.php

david

Aug 6 '06 #1
7 1760
david wrote:
I need 5 queries from the database, which I display in a php foreach
loop the following way.

$query1 = $DB->query("SELECT ... DESC LIMIT 20");
$query2 = $DB->query("SELECT ... DESC LIMIT 20");
$query3 = $DB->query("SELECT ... DESC LIMIT 30");
$query4 = $DB->query("SELECT ... DESC LIMIT 9");
$query5 = $DB->query("SELECT ... DESC LIMIT 9");

foreach ($query1->result as $row) {
echo '<a href="httplink/'.$row['topic_id'].'/"
title="'.htmlspecialchars(trim($row['screen_name'])).':
'.htmlspecialchars(trim($row['title'])).'">&raquo;
'.substr($row['title'], 0, 20)." ...</a><br />
";}
foreach ($query2->result as $row) {
echo '<a href="httplink/'.$row['topic_id'].'/"
title="'.htmlspecialchars(trim($row['screen_name'])).':
'.htmlspecialchars(trim($row['title'])).'">&raquo;
'.substr($row['title'], 0, 20)." ...</a><br />
";}

and so on....

Looking only at performance is there a quicker way to do this? Could I
use a different loop (other than foreach) to get better performance?

I found the following page, but I am not sure which one would help me
and how to change the code around to make it work. Any ideas would help.
Thank you!
http://www.php.lt/benchmark/phpbench.php

david
Why do you want to split hairs? How does 8 ms different than 12 ms in
practical sense?
Aug 6 '06 #2
On 2006-08-06 09:46:02 -0700, s a n j a y <sa***********@gmail.comsaid:
david wrote:
>I need 5 queries from the database, which I display in a php foreach
loop the following way.

$query1 = $DB->query("SELECT ... DESC LIMIT 20");
$query2 = $DB->query("SELECT ... DESC LIMIT 20");
$query3 = $DB->query("SELECT ... DESC LIMIT 30");
$query4 = $DB->query("SELECT ... DESC LIMIT 9");
$query5 = $DB->query("SELECT ... DESC LIMIT 9");

foreach ($query1->result as $row) {
echo '<a href="httplink/'.$row['topic_id'].'/"
title="'.htmlspecialchars(trim($row['screen_name'])).':
'.htmlspecialchars(trim($row['title'])).'">&raquo;
'.substr($row['title'], 0, 20)." ...</a><br />
";}
foreach ($query2->result as $row) {
echo '<a href="httplink/'.$row['topic_id'].'/"
title="'.htmlspecialchars(trim($row['screen_name'])).':
'.htmlspecialchars(trim($row['title'])).'">&raquo;
'.substr($row['title'], 0, 20)." ...</a><br />
";}

and so on....

Looking only at performance is there a quicker way to do this? Could I
use a different loop (other than foreach) to get better performance?

I found the following page, but I am not sure which one would help me
and how to change the code around to make it work. Any ideas would
help. Thank you!
http://www.php.lt/benchmark/phpbench.php

david

Why do you want to split hairs? How does 8 ms different than 12 ms in
practical sense?
Well, when you have hundreds of requests coming in within a short
period of time, those ms add up. This is why I am trying to optimize
this function. Any ideas on how to make this the quickest?

Aug 8 '06 #3
david wrote:
On 2006-08-06 09:46:02 -0700, s a n j a y <sa***********@gmail.comsaid:
>david wrote:
>>I need 5 queries from the database, which I display in a php foreach
loop the following way.

$query1 = $DB->query("SELECT ... DESC LIMIT 20");
$query2 = $DB->query("SELECT ... DESC LIMIT 20");
$query3 = $DB->query("SELECT ... DESC LIMIT 30");
$query4 = $DB->query("SELECT ... DESC LIMIT 9");
$query5 = $DB->query("SELECT ... DESC LIMIT 9");

foreach ($query1->result as $row) {
echo '<a href="httplink/'.$row['topic_id'].'/"
title="'.htmlspecialchars(trim($row['screen_name'])).':
'.htmlspecialchars(trim($row['title'])).'">&raquo;
'.substr($row['title'], 0, 20)." ...</a><br />
";}
foreach ($query2->result as $row) {
echo '<a href="httplink/'.$row['topic_id'].'/"
title="'.htmlspecialchars(trim($row['screen_name'])).':
'.htmlspecialchars(trim($row['title'])).'">&raquo;
'.substr($row['title'], 0, 20)." ...</a><br />
";}

and so on....

Looking only at performance is there a quicker way to do this? Could
I use a different loop (other than foreach) to get better performance?

I found the following page, but I am not sure which one would help me
and how to change the code around to make it work. Any ideas would
help. Thank you!
http://www.php.lt/benchmark/phpbench.php

david

Why do you want to split hairs? How does 8 ms different than 12 ms in
practical sense?


Well, when you have hundreds of requests coming in within a short period
of time, those ms add up. This is why I am trying to optimize this
function. Any ideas on how to make this the quickest?
David,

What do you mean by a "short period"? 1 second? 10 seconds?

Even 100 page requests in 10 seconds would be over 860K requests/day,
which would make your site one of the busiest on the internet. And if
that's the case you'll have a lot more things to worry about -
bandwidth, for instance.

Even at 100 page requests/min. you'll be one of the busiest on the
internet - but the 8ms won't be noticeable.

I understand your desire to optimize the code. But some things just
aren't worth the effort.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 8 '06 #4
I understand your desire to optimize the code. But some things just
aren't worth the effort.
Agreed. Remember, when you optimize you should mostly look at the
complexity. Is it in polynomial or exponential time? If it's
exponential, then you have reason to worry.

For example, which of the following method is the worse? Best?

public function foo($m,$n)
{
for ($x = 0; $x < $m; $x++)
{
echo $x;
}
for ($y = 0; $y < $n; $y++)
{
echo $y;
}
}

public function foo($m,$n)
{
for ($x = 0; $x < $m; $x++)
{
for ($y = 0; $y < $n; $y++)
{
echo $y + $x;
}
}
}

The first one is better; the complexity of the first is O(n+m) vs O(n*m).
Aug 8 '06 #5
Jerry Stuckle wrote:
david wrote:
>On 2006-08-06 09:46:02 -0700, s a n j a y <sa***********@gmail.comsaid:
>>david wrote:

I need 5 queries from the database, which I display in a php foreach
loop the following way.

$query1 = $DB->query("SELECT ... DESC LIMIT 20");
$query2 = $DB->query("SELECT ... DESC LIMIT 20");
$query3 = $DB->query("SELECT ... DESC LIMIT 30");
$query4 = $DB->query("SELECT ... DESC LIMIT 9");
$query5 = $DB->query("SELECT ... DESC LIMIT 9");

Why do you want to split hairs? How does 8 ms different than 12 ms in
practical sense?


Well, when you have hundreds of requests coming in within a short period
of time, those ms add up. This is why I am trying to optimize this
function. Any ideas on how to make this the quickest?

David,

What do you mean by a "short period"? 1 second? 10 seconds?

Even 100 page requests in 10 seconds would be over 860K requests/day,
which would make your site one of the busiest on the internet. And if
that's the case you'll have a lot more things to worry about -
bandwidth, for instance.
No need to *&(% all over his post - how do you know he's NOT doing that
volume?

Faster is not the same thing as more scalable though - its kind of tortoise
and hare thing.

....but if I had to fix the code I'd be looking to do a single query (UNION?)
rather than worrying about which loop construct to use.

C.
Aug 8 '06 #6
Jerry Stuckle wrote:
Even 100 page requests in 10 seconds would be over 860K requests/day,
which would make your site one of the busiest on the internet. And if
that's the case you'll have a lot more things to worry about -
bandwidth, for instance.

Even at 100 page requests/min. you'll be one of the busiest on the
internet - but the 8ms won't be noticeable.

I understand your desire to optimize the code. But some things just
aren't worth the effort.
Right. Nearly all web applications are bound by throughput. Even at
maximum server load CPU utilization won't be near 100%. It's pointless
save a ms here and there when it just end up being idle time.

Aug 8 '06 #7
Colin McKinnon wrote:
Jerry Stuckle wrote:

>>david wrote:
>>>On 2006-08-06 09:46:02 -0700, s a n j a y <sa***********@gmail.comsaid:
david wrote:
>I need 5 queries from the database, which I display in a php foreach
>loop the following way.
>
>$query1 = $DB->query("SELECT ... DESC LIMIT 20");
>$query2 = $DB->query("SELECT ... DESC LIMIT 20");
>$query3 = $DB->query("SELECT ... DESC LIMIT 30");
>$query4 = $DB->query("SELECT ... DESC LIMIT 9");
>$query5 = $DB->query("SELECT ... DESC LIMIT 9");
>

Why do you want to split hairs? How does 8 ms different than 12 ms in
practical sense?
Well, when you have hundreds of requests coming in within a short period
of time, those ms add up. This is why I am trying to optimize this
function. Any ideas on how to make this the quickest?

David,

What do you mean by a "short period"? 1 second? 10 seconds?

Even 100 page requests in 10 seconds would be over 860K requests/day,
which would make your site one of the busiest on the internet. And if
that's the case you'll have a lot more things to worry about -
bandwidth, for instance.


No need to *&(% all over his post - how do you know he's NOT doing that
volume?

Faster is not the same thing as more scalable though - its kind of tortoise
and hare thing.

...but if I had to fix the code I'd be looking to do a single query (UNION?)
rather than worrying about which loop construct to use.

C.
First of all, I didn't "*&(% all over his post" - I pointed out that
he's probably wasting his time trying to save a very little bit of time.

And if he's doing over 800K page views a day (for this one page), he
would have one of the busiest sites on the internet probably on the
order of 20-50M hits a day minimum. He wouldn't be running PHP on a
single server, and probably would not be using MySQL. Rather he would
be running multiple servers with a heavy-duty database such as SQL
Server, Oracle or DB2.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 9 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Simon | last post by:
Hi, If I have a string, (variable len), and I am looking for the first position of one char in array starting from position 'x' For example, // the 'haystack' $string = "PHP is great,...
9
by: Rune Strand | last post by:
Hi, If I have a lot of integers and want do something with each digit as integer, what is the fastest way to get there? Eg. Make 12345 into an iterable object, like or "12345" (Btw: What is...
4
by: laurenq uantrell | last post by:
I am trying to determine which of three stored procedure designs are fastest in the Query Analyzer: One query is a straight SELECT query with all desired rows and a dozen (tblName.RowName =...
5
by: Gustavo Randich | last post by:
Hello, I'm writing an automatic SQL parser and translator from Informix to DB2. Now I'm faced with one of the most difficult things to translate, the "foreach execute procedure" functionality...
11
by: rawCoder | last post by:
Hi, Which is better in terms of performance. Iterating over Enumerator ForNext loop (using indexer) ForEach loop Thanx rawCoder
27
by: Tripper | last post by:
Which is the better way to go and why? //trivial example List<string> strings = GetStrings(); foreach (string s in strings) { // some operation; } strings.ForEach(
6
by: Klaas Vantournhout | last post by:
Hi, I have a question, which is just out of interest. What is the fastest way to do an odd/even check with c++ and if needed assembler. Assume n is an unsigned integer like type (unsigned...
1
by: Dave | last post by:
Hello, What is the best way (performance wise) to search for a row in a table? Let say I have a column named "col1" and what to get the row that the value of "col1" is "It is me"... I can set...
22
by: SETT Programming Contest | last post by:
The SETT Programming Contest: The fastest set<Timplementation Write the fastest set<Timplementation using only standard C++/C. Ideally it should have the same interface like std::set. At least...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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.