C# Best way to get folder depth for a given path?

后端 未结 7 2144
野趣味
野趣味 2021-02-19 18:29

I\'m working on something that requires traversing through the file system and for any given path, I need to know how \'deep\' I am in the folder structure. Here\'s what I\'m cu

7条回答
  •  北海茫月
    2021-02-19 19:02

    Maybe someone need also some performance testing...

            double linqCountTime = 0;
            double stringSplitTime = 0;
            double stringSplitRemEmptyTime = 0;
            int linqCountFind = 0;
            int stringSplitFind = 0;
            int stringSplitRemEmptyFind = 0;
    
            string pth = @"D:\dir 1\complicated dir 2\more complicated dir 3\much more complicated dir 4\only dir\another complicated dir\dummy\dummy.dummy.45682\";
    
            //Heat Up
            DateTime dt = DateTime.Now;
            for (int i = 0; i < 10000; i++)
            {
                linqCountFind = pth.Count(c => c == '\\');
            }
             _= DateTime.Now.Subtract(dt).TotalMilliseconds;
            dt = DateTime.Now;
            for (int i = 0; i < 10000; i++)
            {
                stringSplitFind = pth.Split('\\').Length;
            }
            _ = DateTime.Now.Subtract(dt).TotalMilliseconds;
            dt = DateTime.Now;
            for (int i = 0; i < 10000; i++)
            {
                stringSplitRemEmptyFind = pth.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries).Length;
            }
            _ = DateTime.Now.Subtract(dt).TotalMilliseconds;
            dt = DateTime.Now;
    
            //Testing
            dt = DateTime.Now;
            for (int i = 0; i < 1000000; i++)
            {
                linqCountFind = pth.Count(c => c == '\\');
            }
            linqCountTime = DateTime.Now.Subtract(dt).TotalMilliseconds; //linq.Count: 1390 ms
    
            dt = DateTime.Now;
            for (int i = 0; i < 1000000; i++)
            {
                stringSplitFind = pth.Split('\\').Length-1;
            }
            stringSplitTime = DateTime.Now.Subtract(dt).TotalMilliseconds; //string.Split: 715 ms
    
            dt = DateTime.Now;
            for (int i = 0; i < 1000000; i++)
            {
                stringSplitRemEmptyFind = pth.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries).Length;
            }
            stringSplitRemEmptyTime = DateTime.Now.Subtract(dt).TotalMilliseconds; // string.Split with RemoveEmptyEntries option: 720 ms
    
            string linqCount = "linqCount - Find: "+ linqCountFind + "; Time: "+ linqCountTime.ToString("F0") +" ms"+ Environment.NewLine;
            string stringSplit = "stringSplit - Find: " + stringSplitFind + "; Time: " + stringSplitTime.ToString("F0") + " ms" + Environment.NewLine;
            string stringSplitRemEmpty = "stringSplitRemEmpty - Find: " + stringSplitRemEmptyFind + "; Time: " + stringSplitRemEmptyTime.ToString("F0") + " ms" + Environment.NewLine;
    
            MessageBox.Show(linqCount + stringSplit + stringSplitRemEmpty);
    
            // Results:
            // linqCount - Find: 9;  Time: 1390 ms
            // stringSplit - Find: 9;  Time: 715 ms
            // stringSplitRemEmpty - Find: 9;  Time: 720 ms
    
    • So, for most cases, the best is string.split() (see results in code comments).
    • string.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries) is safer for path.
    • And for more complicated cases see: https://cc.davelozinski.com/c-sharp/fastest-way-count-number-times-character-occurs-string and https://cc.davelozinski.com/c-sharp/c-net-fastest-way-count-substring-occurrences-string

提交回复
热议问题