views:

22

answers:

1

Hi!

I've got a system in which users select a couple of options and receive an image based on those options. I'm trying to combine multiple generated images(corresponding to those options) into the requested picture. I'm trying to optimize this so that if, an image exists for a certain option (i.e. the file exists), then there's no need to compute it and we move on to the next step.

Should I store these images in different folders, where each folder is an option name? Should I store them in the same folder, adding a prefix corresponding to the option to each image? Should I store the filenames in a database and check there? Which way is faster to check a file for existence?

I'm using PHP on Linux, but I'm also interested if the answer varies if I change the programming language or the OS.

+1  A: 

If you're going to be producing a lot of these images, it doesn't seem very scalable to keep them all in one flat directory. I would go with a hierarchy, which will make it a lot easier to manage.

It's always going to be quicker to check in a database than to check if a file exists though, so if speed is the primary concern, use a hierarchical folder structure and keep all the filenames in a database.

Skilldrick
I agree with the first paragraph, but not with the second. A filesystem is, after all, just a specialised kind of database - and it's quite possible - even likely, depending on the filesystem - that checking for a file's existence will be faster than checking for an entry in a database table. Really, the only way to know for sure is to benchmark it.
caf
@caf Fair point. There's also a danger of premature optimisation - maybe just move to a database at the point in which it's really needed, because it may never be (assuming that it would make any difference anyway).
Skilldrick