Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using FizzWare.NBuilder;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- namespace UnitTestProject1
- {
- [TestClass]
- public class FileReadingTest
- {
- [ClassInitialize]
- public static void ClassInitialize(TestContext context)
- {
- Directory.CreateDirectory("Content");
- var generator = new RandomGenerator();
- var numberOfFiles = generator.Next(100, 1000);
- foreach (var file in Enumerable.Range(0, numberOfFiles).Select(x => $"Content/file_{x}.txt"))
- {
- File.WriteAllLines(file, Enumerable.Range(0, generator.Next(100, 1000)).Select(i => generator.Phrase(generator.Next(0, 100))));
- }
- }
- [ClassCleanup]
- public static void ClassCleanup()
- {
- Directory.Delete("Content", true);
- }
- public static Tuple<string[], string[][]> Linq()
- {
- var fileCount = Directory.GetFiles("Content/", "*.txt").Count();
- var fileContents = Enumerable.Range(0, fileCount).Select(i =>
- {
- using (var fileStream = new FileStream($"Content/file_{i}.txt", FileMode.Open))
- using (var streamReader = new StreamReader(fileStream))
- {
- return streamReader.ReadToEnd();
- }
- }).ToArray();
- var fileContentsLines = Enumerable.Range(0, fileCount).Select(i => File.ReadAllLines($"Content/file_{i}.txt")).ToArray();
- return new Tuple<string[], string[][]>(fileContents, fileContentsLines);
- }
- public static Tuple<string[], string[][]> ForUnsafe()
- {
- var fileCount = Directory.GetFiles("Content/", "*.txt").Count();
- var fileContents = new string[fileCount];
- for (var i = 0; i < fileContents.Length; i++)
- {
- var fileStream = new FileStream($"Content/file_{i}.txt", FileMode.Open);
- var streamReader = new StreamReader(fileStream);
- fileContents[i] = streamReader.ReadToEnd();
- streamReader.Dispose();
- fileStream.Dispose();
- }
- var fileContentsLines = new string[fileCount][];
- for (var i = 0; i < fileContentsLines.Length; i++)
- {
- fileContentsLines[i] = File.ReadAllLines($"Content/file_{i}.txt");
- }
- return new Tuple<string[], string[][]>(fileContents, fileContentsLines);
- }
- public static Tuple<string[], string[][]> ForUsing()
- {
- var fileCount = Directory.GetFiles("Content/", "*.txt").Count();
- var fileContents = new string[fileCount];
- for (var i = 0; i < fileContents.Length; i++)
- {
- using (var fileStream = new FileStream($"Content/file_{i}.txt", FileMode.Open))
- using (var streamReader = new StreamReader(fileStream))
- {
- fileContents[i] = streamReader.ReadToEnd();
- }
- }
- var fileContentsLines = new string[fileCount][];
- for (var i = 0; i < fileContentsLines.Length; i++)
- {
- fileContentsLines[i] = File.ReadAllLines($"Content/file_{i}.txt");
- }
- return new Tuple<string[], string[][]>(fileContents, fileContentsLines);
- }
- public static Tuple<string[], string[][]> ForSafe()
- {
- var fileCount = Directory.GetFiles("Content/", "*.txt").Count();
- var fileContents = new string[fileCount];
- for (var i = 0; i < fileContents.Length; i++)
- {
- FileStream fileStream = null;
- StreamReader streamReader = null;
- try
- {
- fileStream = new FileStream($"Content/file_{i}.txt", FileMode.Open);
- streamReader = new StreamReader(fileStream);
- fileContents[i] = streamReader.ReadToEnd();
- }
- finally
- {
- streamReader?.Dispose();
- fileStream?.Dispose();
- }
- }
- var fileContentsLines = new string[fileCount][];
- for (var i = 0; i < fileContentsLines.Length; i++)
- {
- fileContentsLines[i] = File.ReadAllLines($"Content/file_{i}.txt");
- }
- return new Tuple<string[], string[][]>(fileContents, fileContentsLines);
- }
- [TestMethod]
- public void TestMethod1()
- {
- Stopwatch stopWatch;
- stopWatch = Stopwatch.StartNew();
- var forUnsafeResult = ForUnsafe();
- stopWatch.Stop();
- Console.WriteLine($"For Unsafe Time: {stopWatch.ElapsedMilliseconds} ms.");
- stopWatch = Stopwatch.StartNew();
- var forSafeResult = ForSafe();
- stopWatch.Stop();
- Console.WriteLine($"For Safe Time: {stopWatch.ElapsedMilliseconds} ms.");
- stopWatch = Stopwatch.StartNew();
- var linqResult = Linq();
- stopWatch.Stop();
- Console.WriteLine($"Linq Time: {stopWatch.ElapsedMilliseconds} ms.");
- stopWatch = Stopwatch.StartNew();
- var forUsingResult =ForUsing();
- stopWatch.Stop();
- Console.WriteLine($"For Using Time: {stopWatch.ElapsedMilliseconds} ms.");
- CollectionAssert.AreEqual(forUnsafeResult.Item1, forSafeResult.Item1);
- CollectionAssert.AreEqual(forUnsafeResult.Item1, linqResult.Item1);
- CollectionAssert.AreEqual(forUnsafeResult.Item1, forUsingResult.Item1);
- CollectionAssert.AreEqual(forUnsafeResult.Item2.SelectMany(line => line).ToArray(), forSafeResult.Item2.SelectMany(line => line).ToArray());
- CollectionAssert.AreEqual(forUnsafeResult.Item2.SelectMany(line => line).ToArray(), linqResult.Item2.SelectMany(line => line).ToArray());
- CollectionAssert.AreEqual(forUnsafeResult.Item2.SelectMany(line => line).ToArray(), forUsingResult.Item2.SelectMany(line => line).ToArray());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment