代码之家  ›  专栏  ›  技术社区  ›  Miguel

通过本地路径访问文件和通过UNC共享路径访问文件在性能上有什么不同吗

  •  1
  • Miguel  · 技术社区  · 8 年前

    我有几个模板文件,托管在许多地方,主要原因是每个模块都有相应的模板本地访问。

    后来,我意识到这些文件是通过每个箱子的UNC共享路径使用的。

    通过本地路径而不是UNC路径访问本地文件之间是否存在性能差异?

    1 回复  |  直到 8 年前
        1
  •  3
  •   Miguel    8 年前

    看起来开销最小。我使用以下代码运行了一些计时测试:

    var localTimes = new List<long>();
    var sharedTimes = new List<long>();
    
    var stopwatch = new Stopwatch();
    for (int i = 0; i < 1000; i++)
    {
        stopwatch.Start();
        var bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"E:\Shared\Template.xlsx");
        stopwatch.Stop();
    
        localTimes.Add(stopwatch.ElapsedTicks);
    
        stopwatch.Start();
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        bytes = File.ReadAllBytes(@"\\localhost\Shared\Template.xlsx");
        stopwatch.Stop();
    
        sharedTimes.Add(stopwatch.ElapsedTicks);
    }
    
    Console.WriteLine("Local: avg={0}, 50k={1}", TimeSpan.FromTicks((long)localTimes.Average()/50), TimeSpan.FromTicks(localTimes.Sum()));
    Console.WriteLine("Shared: avg={0}, 50k={1}", TimeSpan.FromTicks((long)sharedTimes.Average()/50), TimeSpan.FromTicks(sharedTimes.Sum()));
    

    结果如下:

    我的结论是这无关紧要。