Jump to content

PHP Loop or Mysql Query


BrandonKahre

Recommended Posts

I have a function (2 variations):
[code]function designer_lookup($designer_code) {
    $select = "SELECT `designer_text` FROM `designer_lookup` WHERE `designer_code` = '".$designer_code."' LIMIT 1";
    if ($result = @mysql_query($select)) {
        $fetch = mysql_fetch_array($result);
        return $fetch['designer_text'];
    } else {
        return (boolean)false;
    }
}[/code]

[code]function designer_lookup($designer_code, $designer_lookup_fetch) {
    for ($i=0; $i<count($designer_lookup_fetch); $i++) {
        if (strpos($designer_lookup_fetch[$i]['designer_code'], $designer_code) !== false) {
            return $designer_lookup_fetch[$i]['designer_text'];
        }
    }
    return (boolean)false;
}[/code]

The first one obviously looks up the designer_text by the designer_code by making a mysql query and returning exactly what's needed. However, I can use this function 30+ times in a page. My question is, which function would work fastest?

Notes:
designer_lookup_fetch is a 3d array holding all of the rows and only the two colums (designer_code, designer_text).
the designer_lookup table that I query is currently small, and isn't expected to ever rise above 30 rows.
Also, I have two other functions that are mirrors of this (querying different tables), but they aren't used nearly as often.

Does anyone have any feedback on which function would preform better? Or maybe even another idea completely? My thought is that the first function would obviously become faster (relatively) as the table became bigger.
Link to comment
Share on other sites

If you're running PHP 5, you can test it with the following code:
[code]<?php
$time_start = microtime(true);
// your function and calls go here
$time_end = microtime(true);
$time = $time_end - $time_start;

echo "Ran Analysis in $time seconds<br/><br/>";
?>
[/code]

There is a slight different way to do it if you're running PHP4. Use the following function instead of microtime():
[code]<?php
function microtime_float()
{
   list($usec, $sec) = explode(" ", microtime());
   return ((float)$usec + (float)$sec);
}
?>[/code]

Either way, I think the first one is going to be WAY faster.

Link to comment
Share on other sites

Thanks for the tip, here's the results I got back:
Now I'm never done a real time test like this before, so I did my best to keep the two conditions as equal as possible.[list][*] select first 40 rows of the table[*] for each row, fetch[*] for each fetch, lookup the designer based on the two methods described above[*] run the fetch through a function that echos out the information in a given manner (identical process for both tests)[/list]
for 40 rows:
using queries: Ran Analysis in 0.011135 seconds, 0.011551 seconds, 0.01166 seconds
using an array: Ran Analysis in 0.007887 seconds, 0.007912 seconds, 0.007984 seconds

then I decided to add criteria for the products (in first test there were only 3 designers):
this time the query pulled in 5 designers:
using queries: Ran Analysis in 0.011026 seconds, 0.011846 seconds, 0.012299 seconds
using an array: Ran Analysis in 0.008007 seconds, 0.008027 seconds, 0.007976 seconds


still not satisfied, I had it dump the whole products database (191 rows):
using queries: Ran Analysis in 0.51082 seconds, 0.53646 seconds, 0.436927 seconds
using an array: Ran Analysis in 0.312473 seconds, 0.316537 seconds, 0.398623 seconds

I guess that really answers my question. Let me know if I'm skipping something in my testing enviornment.

Thanks
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.