-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
98 lines (83 loc) · 2.98 KB
/
Program.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
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp17
{
class Program
{
static int N = 10;
static long B = 100;
static long S = 4000 * 256;
static long T = B * S * 4;
static void Main(string[] args)
{
{
Stopwatch w = Stopwatch.StartNew();
TestMemoryMappedFile();
w.Stop();
Console.WriteLine($"MemoryMappedFile: {w.ElapsedMilliseconds}ms");
}
{
Stopwatch w = Stopwatch.StartNew();
TestStream();
w.Stop();
Console.WriteLine($"FileStream: {w.ElapsedMilliseconds}ms");
}
Console.WriteLine("Ready");
Console.ReadKey();
}
static void TestMemoryMappedFile()
{
int[] Data = new int[S];
for (int i = 0; i < Data.Length; i++)
Data[i] = i;
for (int i = 0; i < N; i++)
{
using (var aFileStream = new FileStream($"D:\\file{i}.map", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
{
using (MemoryMappedFile aFile = MemoryMappedFile.CreateFromFile(aFileStream, i.ToString(), 0, MemoryMappedFileAccess.ReadWrite, HandleInheritability.Inheritable, false))
{
using (var aAccessor = aFile.CreateViewAccessor())
{
for (long j = 0; j < T; j += S * 4)
{
int[] ReadData = new int[S * 4];
aAccessor.ReadArray<int>(j, ReadData, 0, ReadData.Length);
}
}
} }
}
}
static unsafe void TestStream()
{
int[] Data = new int[S];
for (int i = 0; i < Data.Length; i++)
Data[i] = i;
byte[] Data2 = new byte[Data.Length * 4];
fixed (int* src = Data) fixed (byte* tgt = Data2)
{
byte* srcptr = (byte*)src;
byte* tgtptr = (byte*)tgt;
for (int i = 0; i < Data2.Length; i++, srcptr++, tgtptr++)
*tgtptr = *srcptr;
}
for (int i = 0; i < N; i++)
{
using (FileStream aFile = new FileStream($"D:\\file{i}.dat", System.IO.FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
{
for (long j = 0; j < B; j++)
{
byte[] ReadData = new byte[S * 4];
aFile.Read(ReadData, 0, ReadData.Length);
}
}
}
}
}
}