Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Logic and efficiency question 1

Status
Not open for further replies.

Ravenstar

Programmer
Feb 22, 2004
4
US
I'm using perl, MySQL and perlmagic to do some work on images from a MySQL database and would like some feedback as to the most efficient way to go about something, I'll do my best to explain. I don't believe this qestion matters much what language or tools I'm using, just the fact that it is a matter of doing a duplicate database query or not.

I have multiple images stored in a database, let's say I need to display 10 of them depending on some variable but some of those images may be duplicates of ones already read in. Should I save copies of the images in memory and check before doing another query to the database, and if I already have the image do not do the query and use that copy, or just go ahead and read it in with another query.

Code looks like this to do as I explain above and check for a duplicate:



# This outer loop I simplified for display here
for ($number = 0; $number < 10; $number++)
{
#############
#loop looking for an image already fetched from the
#database, and if so use it
#############
$match = 0;
for($y = 0; $y < @place; $y++)
{
if ($number eq $place[$y])
{
$image->BlobToImage($holdimages[$y]);
$match = 1;
}
}
###############
if not fetched yet, then go ahead with another query
###############
if (!($match))
{
$sth = $dbh->prepare(qq{select * from table_name where code = '$number'});

$sth->execute();
@data = $sth->fetchrow_array();
$sth->finish();
# $data[1] is the image from the database
$image->BlobToImage($data[1]);
# Saving the image
push (@holdimages, $data[1]);
# Saving it's place in the list and $number
push (@place, $number);
}
}


Any suggestions as to code changes for improved efficiency greatly appreciated or logic changes.

Thx

 
I would say that caching them in memory would certainly be faster, but whether you're able to use that method or not depends on your problem. If these are huge images and/or you're going to be caching a LOT of them in memory, then you might not have the resources to do it. If this is a web thing, and you're really only caching within the set of 10 images that you're loading (and they're not tremendously large) then memory shouldn't be a big issue -- unless you're running a very busy site and lots of people might be doing this simultaneously.

The only thing that I'd really change is the method of storing your cache. Instead of storing an array of image numbers, and iterating through it for each image to be loaded, I would use a hash mapping image number to image data. Hashes are much faster in lookup than iterating through an array. So, replace:
Code:
# Saving the image
push (@holdimages, $data[1]);
# Saving it's place in the list and $number
push (@place, $number);

with:
Code:
$cache{$number} = $data[1];

The test would then replace:
Code:
$match = 0;
for($y = 0; $y < @place; $y++)
{    
  if ($number eq $place[$y])
  {
    $image->BlobToImage($holdimages[$y]);
    $match = 1;
  }
}

if (!($match))
{

with:
Code:
if exists $cache{$number}
{
  $image->BlobToImage($cache{$number});
}
else
{
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top