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

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
 
Store the selected images in an array and compare to that array before either adding the image to the array if not present or selecting another image if it is present....

Don't use perl, PHP guy, and PHP has the in_Array() function that does the compare for ya...

HTH

Bastien

Any one have a techie job in Toronto, I need to work...being laid off sucks!
 
Thank you much, but perl is too big a debate with PHP and from what I've read pretty even split. Besides take me too long to learn PHP for this project, someday maybe. :)

Someone tossed this little tidbit at me in the perl forum:

Save it
$cache{$number} = $data[1];

Then the test:
if exists $cache{$number}
{
$image-&gt;BlobToImage($cache{$number});
}

Works for me.

Thx again
 
I wasn't suggesting you recode in PHP, merely that PHP had a function to do this. I was unsure if Perl had something similar and from your code it does



Bastien

Any one have a techie job in Toronto, I need to work...being laid off sucks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top