Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8927965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:19:40+00:00 2026-06-15T08:19:40+00:00

TL;DR Is it faster to do a file_exists , or checking the return value

  • 0

TL;DR

Is it faster to do a file_exists, or checking the return value from @include (i.e. suppressing errors)?

In this context, you may assume I’m using absolute paths and not relying on include_path resolving.

Elaborate version

With all the conditional code that frameworks and/or software has in their bootstrap process, I started wondering about the fastest way to fail, without sacrificing gracefulness. I have a specific use case in which this thought entered my mind, namely including a config file which doesn’t necessarily exist.

Both operations do a stat anyway, so there’s no way to avoid disk access. The latter option has the added benefit of having read and parsed the file when it succeeds, so in my mind that’s already quite a big bonus.

This is really an extreme micro-optimization, especially in my case where it’s only about one file, but the question still bugs me to death and I don’t know how to test this properly myself.

Brute force testing after accept

After I got a proper answer, I did some brute force testing. I didn’t want to do this before, since I wanted informed answers from the field. Also, I didn’t want to base this solely on my brute force testing, because I’m not at all sure this is the proper way to test this. (Actually, I’m quite certain it isn’t.)

What I did was run the following code snippets:

<?php
$rounds = 1e6;
$start = microtime(true);

while ($rounds--) {
    @include "i-dont-exist";
    clearstatcache();
}

echo microtime(true) - $start, PHP_EOL;

…and…

<?php
$rounds = 1e6;
$start = microtime(true);

while ($rounds--) {
    file_exists("i-dont-exist");
    clearstatcache();
}

echo microtime(true) - $start, PHP_EOL;

…and…

<?php
$rounds = 1e6;
$start = microtime(true);

while ($rounds--) {
    @include "i-exist";
    clearstatcache();
}

echo microtime(true) - $start, PHP_EOL;

…and…

<?php
$rounds = 1e6;
$start = microtime(true);

while ($rounds--) {
    if (file_exists("i-exist")) @include "i-exist";
    clearstatcache();
}

echo microtime(true) - $start, PHP_EOL;

The results were:

Non-existing file

@include: 27.090675830841
file_exists: 1.0596489906311

Existing file

@include: 19.758506059647
file_exists + include: 22.083800077438

What can we conclude from this? Well, at least, as per the links provided in the answer by @goldencrater, suppressing errors is expensive.

In general, I believe suppressing errors is also stupid – unless it’s an informed choice, a criteria which I think my use case validates.

If you’re going to fail after including a including a file fails, in my mind suppressing the error from include is justifiable, since the cost of failing is only inferred on a failing request. (I.e. the error isn’t suppressed and ignored.) Putting a heavy cost on a failing request is much less worse than putting a small cost on every request.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:19:42+00:00Added an answer on June 15, 2026 at 8:19 am

    Even though error supression (@) has a huge overhead in itself, eliminating the file_exists call is always faster.

    The guys behind the Smarty templating engine changed their entire codebase to use @ instead of file_exists (and similar) checks and ended up with a major speed increase. Here’s a blog post from one of the authors detailing the change: http://blog.rodneyrehm.de/archives/12-Improving-Disk-IO-in-PHP-Apps.html .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it better / faster inside an event listener to use this or event.target
Is it generally better/faster to do: if (condition) return a else if (condition2) return
which one is faster select * from parents p inner join children c on
Given input, which shows tag assignments to images, as follows (reading this from php://stdin
I am using fopen() and fread() to read files if( file_exists( $file ) ){
I am using C# to copy files from one directory to another directory. I
I'm using Eclipse/Windows, and it would make things go faster to be able to
Possible Duplicate: Faster DirectoryExists function? I want to check if some file exists on
Possible Duplicate: Which is faster/best? SELECT * or SELECT column1, colum2, column3, etc I
which is better or faster, A or B? std::deque<Myclass> queue; ... // do something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.