static void Main2() { byte[] buffer = new byte[BUFFER_SIZE]; int start, duration; AutoResetEvent turn = new AutoResetEvent(false); // // Open the file. // /* FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, BUFFER_SIZE, AsyncAccess); */ SimpleFileStream fs = new SimpleFileStream(filePath, AsyncAccess); int fileCount = 0; Console.WriteLine("+++start reading the file"); start = Environment.TickCount; // // Define the callback method. // AsyncCallback rdcb = null; rdcb = delegate (IAsyncResult ar) { int bytesRead = 0; try { bytesRead = fs.EndRead(ar); if (bytesRead != 0) { fs.BeginRead(buffer, 0, buffer.Length, rdcb, null); } else { fileCount++; turn.Set(); } } catch (IOException ioex) { Console.WriteLine("*** " + ioex.Message); turn.Set(); } }; for (int i = 0; i < REPEAT_COUNT; i++) { Console.Write("--pass: {0}\r", i); fs.Seek(0L, SeekOrigin.Begin); fs.BeginRead(buffer, 0, buffer.Length, rdcb, null); turn.WaitOne(); } duration = Environment.TickCount - start; int chunks = (int)((fs.Length * fileCount) / BUFFER_SIZE); fs.Close(); Console.WriteLine("---average cost of reading {0}K bytes from a local file: {1:0.0} us", BUFFER_SIZE / 1024, (duration * 1000.0) / chunks); }
static void Main() { byte[] buffer = new byte[BUFFER_SIZE]; int start, duration, fileCount = 0; /* FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, BUFFER_SIZE, AsyncAccess); */ SimpleFileStream fs = new SimpleFileStream(filePath, AsyncAccess); Console.WriteLine("+++start reading the file"); start = Environment.TickCount; for (int i = 0; i < REPEAT_COUNT; i++) { Console.Write("--pass: {0}\r", i + 1); fs.Seek(0L, SeekOrigin.Begin); while (fs.Read(buffer, 0, buffer.Length) != 0) { fileCount++; } } duration = Environment.TickCount - start; int chunks = (int)((fs.Length * fileCount) / BUFFER_SIZE); fs.Close(); Console.WriteLine("\n---average cost of reading {0}K bytes from a local file: {1:0.0} ns", BUFFER_SIZE/1024, (duration * 1000000.0) / chunks); }