Is foreach the fastest way to do this?

This is a discussion on Is foreach the fastest way to do this? within the PHP Language forums, part of the PHP Programming Forums category; I need 5 queries from the database, which I display in a php foreach loop the following way. $query1 = $DB-&...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-06-2006
david
 
Posts: n/a
Default 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

Reply With Quote
  #2 (permalink)  
Old 08-06-2006
s a n j a y
 
Posts: n/a
Default Re: Is foreach the fastest way to do this?

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?
Reply With Quote
  #3 (permalink)  
Old 08-08-2006
david
 
Posts: n/a
Default Re: Is foreach the fastest way to do this?

On 2006-08-06 09:46:02 -0700, s a n j a y <sanjay.debian@gmail.com> said:

> 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?

Reply With Quote
  #4 (permalink)  
Old 08-08-2006
Jerry Stuckle
 
Posts: n/a
Default Re: Is foreach the fastest way to do this?

david wrote:
> On 2006-08-06 09:46:02 -0700, s a n j a y <sanjay.debian@gmail.com> said:
>
>> 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.
jstucklex@attglobal.net
==================
Reply With Quote
  #5 (permalink)  
Old 08-08-2006
Carl Vondrick
 
Posts: n/a
Default Re: Is foreach the fastest way to do this?


> 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).
Reply With Quote
  #6 (permalink)  
Old 08-08-2006
Colin McKinnon
 
Posts: n/a
Default Re: Is foreach the fastest way to do this?

Jerry Stuckle wrote:

> david wrote:
>> On 2006-08-06 09:46:02 -0700, s a n j a y <sanjay.debian@gmail.com> said:
>>
>>> 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.
Reply With Quote
  #7 (permalink)  
Old 08-08-2006
Chung Leong
 
Posts: n/a
Default Re: Is foreach the fastest way to do this?

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.

Reply With Quote
  #8 (permalink)  
Old 08-09-2006
Jerry Stuckle
 
Posts: n/a
Default Re: Is foreach the fastest way to do this?

Colin McKinnon wrote:
> Jerry Stuckle wrote:
>
>
>>david wrote:
>>
>>>On 2006-08-06 09:46:02 -0700, s a n j a y <sanjay.debian@gmail.com> said:
>>>
>>>
>>>>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.
jstucklex@attglobal.net
==================
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:29 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0