Advertisement
NPSF3000

Lots a V3

Oct 30th, 2011
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Diagnostics;
  7.  
  8. //  This is due to a wierd unity3d forum question + late night.
  9. //  I write in 71s
  10. //  And read in 64.59s
  11. //  On a Samsung 1TB F3 while doing basic background tasks.
  12.  
  13. namespace WriteLotsOfV3
  14. {
  15.     class Program
  16.     {
  17.         static void Main(string[] args)
  18.         {
  19.             Console.WriteLine("[R]ead or [W]rite?");
  20.  
  21.             char selection = Console.ReadLine().ToLower()[0];
  22.  
  23.             if (selection == 'w')
  24.             {
  25.                 Console.WriteLine();
  26.                 Console.WriteLine("Writing");
  27.                 Console.WriteLine();
  28.  
  29.                 Int32 iterations = 67108864;
  30.  
  31.                 var sw = Stopwatch.StartNew();
  32.  
  33.                 var fs = new FileStream("Vector3s.dat", FileMode.Create);
  34.                 var bw = new BinaryWriter(fs);
  35.  
  36.                 var rnd = new Random();
  37.  
  38.                 bw.Write(iterations);
  39.  
  40.                 for (int i = 0; i < iterations; i++)
  41.                 {
  42.                     for (int x = 0; x < 3; x++)
  43.                         bw.Write(NextFloat(rnd));
  44.                 }
  45.                 fs.Flush(true);
  46.  
  47.                 sw.Stop();
  48.  
  49.                 Console.WriteLine(fs.Name);
  50.  
  51.                 Console.WriteLine();
  52.  
  53.                 Console.WriteLine("Finished writing in {0}ms", sw.ElapsedMilliseconds);
  54.  
  55.  
  56.                 fs.Dispose();
  57.                 bw.Dispose();
  58.             }
  59.             else if (selection == 'r')
  60.             {
  61.                 Console.WriteLine();
  62.                 Console.WriteLine("Reading");
  63.                 Console.WriteLine();
  64.  
  65.                 var fs1 = new FileStream("Vector3s.dat", FileMode.Open);
  66.                 var br = new BinaryReader(fs1);
  67.  
  68.                 var sw = Stopwatch.StartNew();
  69.                 int iterations = br.ReadInt32();
  70.  
  71.                 var data = new float[iterations, 3];
  72.  
  73.                 for (int i = 0; i < iterations; i++)
  74.                 {
  75.                     for (int x = 0; x < 3; x++)
  76.                         data[i, x] = br.ReadSingle();
  77.                 }
  78.  
  79.                 sw.Stop();
  80.  
  81.                 Console.WriteLine("Finished reading in {0}ms", sw.ElapsedMilliseconds);
  82.                 Console.ReadLine();
  83.             }
  84.             else { Console.WriteLine("Invalid"); Console.WriteLine("");  Main(null); }
  85.         }
  86.  
  87.         //http://stackoverflow.com/questions/3365337/best-way-to-generate-a-random-float-in-c-sharp
  88.         static float NextFloat(Random random)
  89.         {
  90.             double mantissa = (random.NextDouble() * 2.0) - 1.0;
  91.             double exponent = Math.Pow(2.0, random.Next(-126, 128));
  92.             return (float)(mantissa * exponent);
  93.         }
  94.     }
  95.  
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement