file_exists() is too slow in PHP. Can anyone suggest a faster alternative?

后端 未结 19 1063
迷失自我
迷失自我 2021-02-05 01:18

When displaying images on our website, we check if the file exists with a call to file_exists(). We fall back to a dummy image if the file was missing.

Howe

19条回答
  •  后悔当初
    2021-02-05 01:47

    The fastest way to check existence of a local file is stream_resolve_include_path():

    if (false !== stream_resolve_include_path($s3url)) { 
      //do stuff 
    }
    

    Performance results stream_resolve_include_path() vs file_exists():

    Test name       Repeats         Result          Performance     
    stream_resolve  10000           0.051710 sec    +0.00%
    file_exists     10000           0.067452 sec    -30.44%
    

    In test used absolute paths. Test source is here. PHP version:

    PHP 5.4.23-1~dotdeb.1 (cli) (built: Dec 13 2013 21:53:21)
    Copyright (c) 1997-2013 The PHP Group
    Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

提交回复
热议问题