Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by prabab · Apr 10, 2013 at 10:45 PM · slowmethodsystem.io

Fast method of checking if file exists

Hi. In my project I need to check if a specific file exists before I try to load it and perform some operations on it in runtime.

Using (System.IO.File.Exists(dir)) method works, however, for some reason it's very slow and takes up to one second. I'd say that's definitely too long to make any sense considering that the operation triggered by this condition lasts for less than a millisecond.

Are there any faster methods of checking if a file exists?

I've been thinking about preparing a file list at program launch, which would make runtime operations faster, as the condition would then compare with a value from the memory. But that would be sweeping the problem under the carpet instead of getting rid of it.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Benproductions1 · Apr 11, 2013 at 05:24 AM 1
Share

Why does it take 1 second? wtf for me it takes 1ms!

avatar image whydoidoit · Apr 11, 2013 at 05:26 AM 0
Share

Is that file on a network or something? Have you given it a path that the system might think needs network resolution?

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by prabab · Apr 11, 2013 at 05:40 AM

It's a local file. Anyways, I found factors which were the cause:

  1. Searching for files with characters like "-" and "_" in the file name drastically raise the search time for some reason.

  2. Looking for a file that doesn't exist takes a lot of time, which makes sense, as the routine probably has to check everything in the folder.

  3. When there are lots of files in the folder (few thousands in my case) the search additionally slows down.

Eventually I've decided to list all files with Directory.GetFiles() at program launch and put their shortened names into a dictionary. Now single search takes about 0.000007 seconds.

I guess it was my fault that I was trying to use method which obviously wasn't suited for such use. But, hey, at least I learned something new today.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Apr 11, 2013 at 05:41 AM 1
Share

I've converted your comment to an answer - you should tick it as being correct.

avatar image
0

Answer by astracat111 · Oct 17, 2016 at 04:08 PM

I think that fileexists is more for if you can throw up a loading screen.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

App slow to return to AR scene 0 Answers

Variable Question. [Simple] & [JS] 1 Answer

Unity Lags when editing terrain?? 0 Answers

Does these codes slow down my game? 1 Answer

NVIDIA GTX 1080 and Unity3D editor incompatibility 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges