简体   繁体   English

使用元数据提取器提取 Exif 数据时性能下降

[英]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 .对于拍照日期,我使用的是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,我在这个块周围包裹了一个Stopwatch object 来测量性能,这是我得到的结果,

Processed 2244 files in 440218ms.在 440218 毫秒内处理了 2244 个文件。

If I comment out the Exif code (the try-catch block) I get,如果我注释掉 Exif 代码( try-catch块)我得到,

Processed 2244 files in 116928ms.在 116928 毫秒内处理了 2244 个文件。

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,编辑根据反馈,我已切换到使用StreamWriter ,如下所示,

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,基于这个最新的变化,使用 Exif 代码所花费的时间减少了一半,

Processed 2244 files in 212278ms.在 212278 毫秒内处理了 2244 个文件。

Based on following comments from Drew Noakes ,根据Drew Noakes的以下评论,

Performance in your case is likely down to I/O.在您的情况下,性能可能取决于 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如果您只想要 JPEG 文件的一天 Exif,它可能会提高性能

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.与其在循环中调用File.AppendAllText ,不如创建一个StreamWriter并使用它,这样您只需打开和关闭 output 文件一次,而不是每个输入文件一次。 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.请参阅原始帖子中的编辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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