# File.exists? performance

**URL:** <https://www.ruby-forum.com/t/file-exists-performance/175910>\
**Category:** Ruby\
**Created:** [September 22, 2009, 6:27pm UTC](https://www.ruby-forum.com/t/file-exists-performance/175910 "2009-09-22T18:27:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![arvias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@arvias](https://www.ruby-forum.com/u/arvias)\
**Post date:** [September 22, 2009, 6:27pm UTC](https://www.ruby-forum.com/t/file-exists-performance/175910/1 "2009-09-22T18:27:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![arvias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@arvias](https://www.ruby-forum.com/u/arvias)\
**Post date:** [September 22, 2009, 6:37pm UTC](https://www.ruby-forum.com/t/file-exists-performance/175910/2 "2009-09-22T18:37:59Z")

</div>

> How CPU intensive is File.exists? ?

I think I should have said I/O.

---

<div class="post-metadata">

**Author:** ![arvias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@arvias](https://www.ruby-forum.com/u/arvias)\
**Post date:** [September 23, 2009, 12:55am UTC](https://www.ruby-forum.com/t/file-exists-performance/175910/3 "2009-09-23T00:55:37Z")

</div>

-------- Original-Nachricht --------

> Datum: Wed, 23 Sep 2009 01:34:59 +0900  
> Von: Marcelo B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> An: removed\_email\_address@domain.invalid  
> Betreff: Re: File.exists? performance

> > How CPU intensive is File.exists? ?
> 
> I think I should have said I/O.
> 
> –  
> M.

Dear Marcelo,

you might use profiling with,e.g., ruby-prof.

> **[Profile and ruby-prof](http://on-ruby.blogspot.com/2006/08/profile-and-ruby-prof.html)**
>
> I've been playing with profile (from the Standard Library) and ruby-prof (the profile replacement written by Shugo Maeda and Charlie Savage...

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![arvias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@arvias](https://www.ruby-forum.com/u/arvias)\
**Post date:** [September 23, 2009, 3:20am UTC](https://www.ruby-forum.com/t/file-exists-performance/175910/4 "2009-09-23T03:20:22Z")

</div>

On Tuesday 22 September 2009 11:34:59 am Marcelo B. wrote:

> > How CPU intensive is File.exists? ?
> 
> 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 E. 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.
