Guest User

Untitled

a guest
Oct 9th, 2016
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using FizzWare.NBuilder;
  7. using Microsoft.VisualStudio.TestTools.UnitTesting;
  8.  
  9. namespace UnitTestProject1
  10. {
  11.     [TestClass]
  12.     public class FileReadingTest
  13.     {
  14.         [ClassInitialize]
  15.         public static void ClassInitialize(TestContext context)
  16.         {
  17.             Directory.CreateDirectory("Content");
  18.  
  19.             var generator = new RandomGenerator();
  20.  
  21.             var numberOfFiles = generator.Next(100, 1000);
  22.  
  23.             foreach (var file in Enumerable.Range(0, numberOfFiles).Select(x => $"Content/file_{x}.txt"))
  24.             {
  25.                 File.WriteAllLines(file, Enumerable.Range(0, generator.Next(100, 1000)).Select(i => generator.Phrase(generator.Next(0, 100))));
  26.             }
  27.  
  28.         }
  29.  
  30.         [ClassCleanup]
  31.         public static void ClassCleanup()
  32.         {
  33.             Directory.Delete("Content", true);
  34.         }
  35.  
  36.         public static Tuple<string[], string[][]> Linq()
  37.         {
  38.             var fileCount = Directory.GetFiles("Content/", "*.txt").Count();
  39.  
  40.             var fileContents = Enumerable.Range(0, fileCount).Select(i =>
  41.             {
  42.                 using (var fileStream = new FileStream($"Content/file_{i}.txt", FileMode.Open))
  43.                 using (var streamReader = new StreamReader(fileStream))
  44.                 {
  45.                     return streamReader.ReadToEnd();
  46.                 }
  47.             }).ToArray();
  48.  
  49.             var fileContentsLines = Enumerable.Range(0, fileCount).Select(i => File.ReadAllLines($"Content/file_{i}.txt")).ToArray();
  50.  
  51.             return new Tuple<string[], string[][]>(fileContents, fileContentsLines);
  52.         }
  53.  
  54.         public static Tuple<string[], string[][]> ForUnsafe()
  55.         {
  56.             var fileCount = Directory.GetFiles("Content/", "*.txt").Count();
  57.  
  58.             var fileContents = new string[fileCount];
  59.  
  60.             for (var i = 0; i < fileContents.Length; i++)
  61.             {
  62.                 var fileStream = new FileStream($"Content/file_{i}.txt", FileMode.Open);
  63.                 var streamReader = new StreamReader(fileStream);
  64.                 fileContents[i] = streamReader.ReadToEnd();
  65.                 streamReader.Dispose();
  66.                 fileStream.Dispose();
  67.             }
  68.  
  69.             var fileContentsLines = new string[fileCount][];
  70.  
  71.             for (var i = 0; i < fileContentsLines.Length; i++)
  72.             {
  73.                 fileContentsLines[i] = File.ReadAllLines($"Content/file_{i}.txt");
  74.             }
  75.  
  76.             return new Tuple<string[], string[][]>(fileContents, fileContentsLines);
  77.         }
  78.  
  79.         public static Tuple<string[], string[][]> ForUsing()
  80.         {
  81.             var fileCount = Directory.GetFiles("Content/", "*.txt").Count();
  82.  
  83.             var fileContents = new string[fileCount];
  84.  
  85.             for (var i = 0; i < fileContents.Length; i++)
  86.             {
  87.                 using (var fileStream = new FileStream($"Content/file_{i}.txt", FileMode.Open))
  88.                 using (var streamReader = new StreamReader(fileStream))
  89.                 {
  90.                     fileContents[i] = streamReader.ReadToEnd();
  91.                 }
  92.             }
  93.  
  94.             var fileContentsLines = new string[fileCount][];
  95.  
  96.             for (var i = 0; i < fileContentsLines.Length; i++)
  97.             {
  98.                 fileContentsLines[i] = File.ReadAllLines($"Content/file_{i}.txt");
  99.             }
  100.  
  101.             return new Tuple<string[], string[][]>(fileContents, fileContentsLines);
  102.         }
  103.  
  104.         public static Tuple<string[], string[][]> ForSafe()
  105.         {
  106.             var fileCount = Directory.GetFiles("Content/", "*.txt").Count();
  107.  
  108.             var fileContents = new string[fileCount];
  109.  
  110.             for (var i = 0; i < fileContents.Length; i++)
  111.             {
  112.                 FileStream fileStream = null;
  113.                 StreamReader streamReader = null;
  114.  
  115.                 try
  116.                 {
  117.                     fileStream = new FileStream($"Content/file_{i}.txt", FileMode.Open);
  118.                     streamReader = new StreamReader(fileStream);
  119.                     fileContents[i] = streamReader.ReadToEnd();
  120.                 }
  121.                 finally
  122.                 {
  123.                     streamReader?.Dispose();
  124.                     fileStream?.Dispose();
  125.                 }
  126.             }
  127.  
  128.             var fileContentsLines = new string[fileCount][];
  129.  
  130.             for (var i = 0; i < fileContentsLines.Length; i++)
  131.             {
  132.                 fileContentsLines[i] = File.ReadAllLines($"Content/file_{i}.txt");
  133.             }
  134.  
  135.             return new Tuple<string[], string[][]>(fileContents, fileContentsLines);
  136.         }
  137.  
  138.         [TestMethod]
  139.         public void TestMethod1()
  140.         {
  141.             Stopwatch stopWatch;
  142.  
  143.             stopWatch = Stopwatch.StartNew();
  144.             var forUnsafeResult = ForUnsafe();
  145.             stopWatch.Stop();
  146.  
  147.             Console.WriteLine($"For Unsafe Time: {stopWatch.ElapsedMilliseconds} ms.");
  148.  
  149.             stopWatch = Stopwatch.StartNew();
  150.             var forSafeResult = ForSafe();
  151.             stopWatch.Stop();
  152.  
  153.             Console.WriteLine($"For Safe Time: {stopWatch.ElapsedMilliseconds} ms.");
  154.  
  155.             stopWatch = Stopwatch.StartNew();
  156.             var linqResult = Linq();
  157.             stopWatch.Stop();
  158.  
  159.             Console.WriteLine($"Linq Time: {stopWatch.ElapsedMilliseconds} ms.");
  160.  
  161.             stopWatch = Stopwatch.StartNew();
  162.             var forUsingResult =ForUsing();
  163.             stopWatch.Stop();
  164.  
  165.             Console.WriteLine($"For Using Time: {stopWatch.ElapsedMilliseconds} ms.");
  166.  
  167.             CollectionAssert.AreEqual(forUnsafeResult.Item1, forSafeResult.Item1);
  168.             CollectionAssert.AreEqual(forUnsafeResult.Item1, linqResult.Item1);
  169.             CollectionAssert.AreEqual(forUnsafeResult.Item1, forUsingResult.Item1);
  170.  
  171.             CollectionAssert.AreEqual(forUnsafeResult.Item2.SelectMany(line => line).ToArray(), forSafeResult.Item2.SelectMany(line => line).ToArray());
  172.             CollectionAssert.AreEqual(forUnsafeResult.Item2.SelectMany(line => line).ToArray(), linqResult.Item2.SelectMany(line => line).ToArray());
  173.             CollectionAssert.AreEqual(forUnsafeResult.Item2.SelectMany(line => line).ToArray(), forUsingResult.Item2.SelectMany(line => line).ToArray());
  174.         }
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment