-
Notifications
You must be signed in to change notification settings - Fork 0
/
BenchmarkCheckFileExists.cs
85 lines (76 loc) · 2.65 KB
/
BenchmarkCheckFileExists.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
namespace FileExistsBenchmark
{
//[ShortRunJob]
public class BenchmarkCheckFileExists
{
[System.Runtime.InteropServices.DllImport("shlwapi.dll", EntryPoint = "PathFileExistsW", SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool _PathFileExists([MarshalAs(UnmanagedType.LPWStr)]string pszPath);
public string[] FileNames => new string[]
{
@"C:\TEMP\test.txt", // File exists
@"C:\TEMP\test.not", // File not exist
@"\\10.187.39.110\Temp\test.txt", // File exists
@"\\10.187.39.110\Temp\test.not", // File not exists
@"Z:\test.txt", // File exists, map Z: \\10.187.39.110\Temp
@"Z:\test.not", // File not exists, map Z: \\10.187.39.110\Temp
@"J:\test.not", // File not exists, network drive is inactive
};
[Benchmark(Baseline = true)]
[ArgumentsSource(nameof(FileNames))]
public bool File_Exists(string fileName) => File.Exists(fileName);
[Benchmark]
[ArgumentsSource(nameof(FileNames))]
public bool PathFileExists(string fileName) => _PathFileExists(fileName);
[Benchmark]
[ArgumentsSource(nameof(FileNames))]
public bool FileInfo(string fileName) => (new FileInfo(fileName)).Exists;
[Benchmark]
[ArgumentsSource(nameof(FileNames))]
public bool FileInfo_TryCatch(string fileName)
{
try
{
return (new FileInfo(fileName)).Exists;
}
catch (Exception)
{
return false;
}
}
[Benchmark]
[ArgumentsSource(nameof(FileNames))]
public bool FileOpenRead(string fileName)
{
try
{
using (File.OpenRead(fileName))
{
return true;
}
}
catch (Exception)
{
return false;
}
}
[Benchmark]
[ArgumentsSource(nameof(FileNames))]
// Alternative based on http://www.jonathanantoine.com/2011/08/18/faster-file-exists/
public bool PathFileExistsAsync(string fileName)
{
var task = new Task<bool>(() =>
{
return PathFileExists(fileName);
});
task.Start();
return task.Wait(5000) && task.Result;
}
}
}