简体   繁体   中英

Slow performance when extracting Exif data using metadata-extractor

I have a fairly simple task - from a collection of images I need to extract the Creation Date, the Last Modified Date and the Picture Taken Date and write them to a text file. For the Picture Taken Date, I am using metadata-extractor .

This is some sample code,

    List<FileInfo> fileList = Utils.FileList(targetPath, true);
    foreach (FileInfo fi in fileList)
    {
        string dateTime = "";
        try
        {
            var metadatadir = ImageMetadataReader.ReadMetadata(fi.FullName);
            var subIfdDirectory = metadatadir.OfType<ExifSubIfdDirectory>().FirstOrDefault();
            dateTime = subIfdDirectory?.GetDescription(ExifDirectoryBase.TagDateTimeOriginal);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
        DateTime creationDate = File.GetCreationTime(fi.FullName);
        DateTime modifiedDate = File.GetLastWriteTime(fi.FullName);
        string outputLine = "Filename " + fi.Name + "\t Creation Time: " + creationDate +
            "\t Modified Time: " + modifiedDate + "\t" + "Exif Time: " + dateTime + "\n";
        File.AppendAllText(targetFile, outputLine);
        fileCount++;
    }

I wrap a Stopwatch object around this block to measure performance and this is the result I get,

Processed 2244 files in 440218ms.

If I comment out the Exif code (the try-catch block) I get,

Processed 2244 files in 116928ms.

Am I using the library incorrectly? Is there a faster way of pulling the data out?

EDIT Based on feedback I have switched to using StreamWriter as below,

using (StreamWriter tFile = File.AppendText(targetFile))
{
    // Code
    tFile.WriteLine(outputLine);
}

Based on this latest change the time taken has been cut in half with the Exif code,

Processed 2244 files in 212278ms.

Based on following comments from Drew Noakes ,

Performance in your case is likely down to I/O. Now the code you're using pulls out all metadata types for all file formats. If you only want, day, Exif for JPEG files, itmay be possible to improve performance

Instead of calling File.AppendAllText in the loop, create a StreamWriter and spend to that, so that you only open and close the output file once rather than once per input file. It will allow more buffering too.

I have done exactly this and performance speed has doubled, which is good enough for my purposes. See edit in the original post.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM