2017-12-07 10 views
0

サブパスのリスト(行)をList<FileInfo>と比較し、リストに欠損エントリを追加する以下の方法があります。_missingImageFolderEnteries私はLINQを使用しており、処理が驚くほど遅いように見えます。どのようにアイデアを考えているのか、パフォーマンスを改善する方法を考えていますか?おそらくLINQはここにはありませんが、私は標準的なループよりも効率的でなければならないと考えました。FileInfoのリストと比較したサブパス文字列のリスト

private List<FileInfo> _imageFilesInFolderPathList = new List<FileInfo>(); 
private readonly List<Tuple<string, string>> _missingImageFolderEnteries = new List<Tuple<string, string>>(); 
private void CrossCheckImageWithFolder(EnumerableRowCollection<DataRow> Rows) 
{ 
    foreach (var dr in Rows) 
    { 
     var filepath = dr[ImagePathHeader].ToString(); 

     if (!_imageFilesInFolderPathList.Any(row => 
      row.FullName.EndsWith(FilePathHandler.FormatSubPath(filepath)))) 
      _missingImageFolderEnteries.Add(
       new Tuple<string, string>(
        filepath.Substring(CommonInitFolder.Length).StartsWith("\\") 
         ? filepath.Substring(CommonInitFolder.Length) 
         : "\\" + filepath.Substring(CommonInitFolder.Length), 
        " does not exist in image folder")); 
    } 

    _missingImageFolderEnteries.Sort((x, y) => y.Item1.CompareTo(x.Item1)); 
} 

public static string FormatSubPath(string path, bool needsSlash = true) 
{ 
    var returnPath = path; 
    if (returnPath.StartsWith(".")) 
     returnPath = returnPath.Substring(1); 
    if (!returnPath.StartsWith("\\") && needsSlash) 
     returnPath = "\\" + returnPath; 
    else if (returnPath.StartsWith("\\") && !needsSlash) 
     returnPath = returnPath.Substring(1); 

    return returnPath; 
} 
+0

_「標準ループよりも効率的でなければならないと思った」_ LINQはループを隠すだけです。実際には、古典的なループよりもLINQで非効率なコードを書く方が簡単です。しかし、LINQを使って効率的なコードを書くことができます。 –

答えて

1

Linqは抽象度のレベルなので、手動ループよりも高速になることはありません。

私は_imageFilesInFolderPathListをファイルの名前を含むHashSet<string>に変換しようとします。また、パフォーマンスの向上を得るために、EndsWithを使用してinsting全体の文字列を比較する必要があります。

+0

私が 'EndsWith'を使う理由は、パス文字列(dr)がどのくらいの長さになるかわからない、つまり絶対パスではないからです。 – windowskm

関連する問題