Search code examples
calgorithmsystem

which one will be more efficient in searching of a certain type of file?


Suppose I'm designing a file manager and want to implement searching of a file by its type hypothetically then which one of these methods will be more efficient -

  1. use the name of the file and trim the extension of each file.

  2. use of specific bytes for the type of file we are searching for example in the case of jpeg images.

bytes 0xFF, 0xD8 indicate start of image

bytes 0xFF, 0xD9 indicate end of image

Solution

  • Since you have to know it's filename before open it, the name trim option will be probably faster. However, you could have false results with that method if an extension does not match with actual file type.

    Doing that way will save you some system calls (open, read, maybe fseek, close).