how to possibly cache something and the theory of better performance / better speed

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi All,

 

This is 2 parts, first "theory"

 

I found this code on the web tossed it into a site to test it and it seems
pretty efficient.  Sometimes when sites are small without a lot of traffic
efficiency isn't a big issue and a lot of people don't pay attention to
these details.  I just like keeping things clean and efficient so here goes
the theory part.

 

I used to use a script to grab a random image from a folder of images by
scanning the folder, returning the list of images, getting one of them
randomly and displaying it.

This page of course had to be a .php page so everything runs through the php
engine taking some processing power to go through the regular html code in
order to run the php stuff.

 

This script used the concept of running the page as an html page and calling
the php script like so:

<img src="/images/rotate.php">

 

This would call the below .php file in order to execute the list of files in
the folder and display the random image.

 

<?php

$folder = '';

$exts = 'jpg jpeg png gif';

 

$files = array(); $i = -1;

if ('' == $folder) $folder = './';

 

$handle = opendir($folder);

$exts = explode(' ', $exts);

 

while (false !== ($file = readdir($handle))) {

  foreach($exts as $ext) {

    if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg,
case insensitive

      $files[] = $file;

      ++$i;

    }

  }

}

 

closedir($handle);

mt_srand((double)microtime()*1000000);

$rand = mt_rand(0, $i); 

header('Location: '.$folder.$files[$rand]);

?>

 

 

Conceptually that seems to make it a little more efficient.

Does everyone agree?

 

 

OK now, getting greedy I want to take it to another level.  Instead of
having to read the folder for images every time, why not read the image
names into a file so that we can maintain therbey caching the list.  This is
based on 25-30 images in the folder you might even have more.  Since in this
case we're not going to add images too often, then we can upload the images
delete the cache list and a new one will be generated hopefully saving time
for all future executions.

 

We would also have to modify the above code to check if the file exists, if
not generate the file and if it's there just display the images.

 

Assuming only 25-30 images am I splitting hairs here? Does this make sense?
Of course if I have 200 pictures, then this is the way to go but would this
really make a difference?

 

Thanks!

 

Joey

 

 

 

 

 


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux