File.exists? performance

M

Marcelo Barbudas

Hi.

How CPU intensive is File.exists? ?

I am thinking of adding theme support in my rails project. I want it to
check if theme image exists and if it doesn't display a default one.

This would mean a File.exists? for each image_tag call.
 
D

David Masover

I think I should have said I/O.

It depends entirely what you're doing.

But for what you're talking about, it really can't be much. Think of it this
way: How many images are you talking about? If your server has anything close
to a decent amount of RAM, all of those are going to be cached. You're already
doing FAR more than checking if a file exists (basically a stat(2) call, by my
count it's going to be at most 52 _bytes_) than you will if you have to
actually read that image. Worst case, when someone only does a HEAD on an
image, you'll be doing two stat calls, so it'll be about half the usual speed.

In other words, do what Axel Etzold suggested -- or better yet, don't worry
about it. Do it this way, and rethink your architecture (or add some sort of
caching) if it becomes a problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,109
Latest member
JanieMalco
Top