Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Diagnostics;
- // This is due to a wierd unity3d forum question + late night.
- // I write in 71s
- // And read in 64.59s
- // On a Samsung 1TB F3 while doing basic background tasks.
- namespace WriteLotsOfV3
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("[R]ead or [W]rite?");
- char selection = Console.ReadLine().ToLower()[0];
- if (selection == 'w')
- {
- Console.WriteLine();
- Console.WriteLine("Writing");
- Console.WriteLine();
- Int32 iterations = 67108864;
- var sw = Stopwatch.StartNew();
- var fs = new FileStream("Vector3s.dat", FileMode.Create);
- var bw = new BinaryWriter(fs);
- var rnd = new Random();
- bw.Write(iterations);
- for (int i = 0; i < iterations; i++)
- {
- for (int x = 0; x < 3; x++)
- bw.Write(NextFloat(rnd));
- }
- fs.Flush(true);
- sw.Stop();
- Console.WriteLine(fs.Name);
- Console.WriteLine();
- Console.WriteLine("Finished writing in {0}ms", sw.ElapsedMilliseconds);
- fs.Dispose();
- bw.Dispose();
- }
- else if (selection == 'r')
- {
- Console.WriteLine();
- Console.WriteLine("Reading");
- Console.WriteLine();
- var fs1 = new FileStream("Vector3s.dat", FileMode.Open);
- var br = new BinaryReader(fs1);
- var sw = Stopwatch.StartNew();
- int iterations = br.ReadInt32();
- var data = new float[iterations, 3];
- for (int i = 0; i < iterations; i++)
- {
- for (int x = 0; x < 3; x++)
- data[i, x] = br.ReadSingle();
- }
- sw.Stop();
- Console.WriteLine("Finished reading in {0}ms", sw.ElapsedMilliseconds);
- Console.ReadLine();
- }
- else { Console.WriteLine("Invalid"); Console.WriteLine(""); Main(null); }
- }
- //http://stackoverflow.com/questions/3365337/best-way-to-generate-a-random-float-in-c-sharp
- static float NextFloat(Random random)
- {
- double mantissa = (random.NextDouble() * 2.0) - 1.0;
- double exponent = Math.Pow(2.0, random.Next(-126, 128));
- return (float)(mantissa * exponent);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement