Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cosminpopescu14
Created November 9, 2019 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosminpopescu14/ff7a053ec62926ada35f76b4602a1321 to your computer and use it in GitHub Desktop.
Save cosminpopescu14/ff7a053ec62926ada35f76b4602a1321 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using System.IO;
using System.Security.Cryptography;
namespace Bench
{
class Program
{
private static readonly Random random = new Random();
static void Main(string[] args)
{
//int result = 0;
var sw = new Stopwatch();
sw.Start();
/*for (int i = 1000000000; i != 0; i--)
{
result++;
}
Console.WriteLine("Value: {0} Took: {1}s", result, sw.ElapsedMilliseconds / 1000F);*/
for (int steps = 0; steps < 1000000000; ++steps)
{
GenerateRandomsPair();
}
Console.WriteLine("Took: {0}s", sw.Elapsed / 1000F);
}
private static void GenerateSecureRandomPair()
{
using (RNGCryptoServiceProvider rg = new RNGCryptoServiceProvider())
{
byte[] rno = new byte[4];
byte[] rno1 = new byte[4];
rg.GetBytes(rno);
rg.GetBytes(rno1);
int randomvalue = Math.Abs(BitConverter.ToInt32(rno, 0));
int randomvalue1 = Math.Abs(BitConverter.ToInt32(rno1, 0));
//Console.WriteLine("{0} {1}",randomvalue, randomvalue1);
WriteInFile(randomvalue, randomvalue1);
}
}
private static void GenerateRandomsPair()
{
lock (random)
{
WriteInFile(random.Next(0, Int32.MaxValue), random.Next(0, Int32.MaxValue));
}
}
private async static void WriteInFile(int nb1, int nb2)
{
using (StreamWriter outputFile = new StreamWriter("WriteTextAsync.txt", append: true))
{
await outputFile.WriteAsync(nb1 + "\t" + nb2 + "\t" + (float)nb1 / (float)nb2);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment