Skip to content

Instantly share code, notes, and snippets.

@bernardobrezende
Created November 25, 2011 17:04
Show Gist options
  • Save bernardobrezende/1393980 to your computer and use it in GitHub Desktop.
Save bernardobrezende/1393980 to your computer and use it in GitHub Desktop.
Microbenchmark para escrita/leitura em hashtable
using System;
using System.Diagnostics;
namespace MicroBenchmark.Hashtable
{
class Program
{
const int HASH_SIZE = 1000000;
static Guid keyToRetrieve;
static void Main(string[] args)
{
Stopwatch writeTimeCounter = new Stopwatch();
Stopwatch readTimeCounter = new Stopwatch();
//
writeTimeCounter.Start();
System.Collections.Hashtable hash = PopulateHash();
writeTimeCounter.Stop();
//
readTimeCounter.Start();
byte[] result = (byte[])hash[keyToRetrieve];
//
readTimeCounter.Stop();
Console.WriteLine("Tempo para escrita no hash: {0}", writeTimeCounter.Elapsed);
Console.WriteLine("Tempo para leitura do hash: {0}", readTimeCounter.Elapsed);
Console.Read();
}
static System.Collections.Hashtable PopulateHash()
{
System.Collections.Hashtable hash = new System.Collections.Hashtable(HASH_SIZE);
for (int i = 0; i < HASH_SIZE; i++)
{
Guid key = Guid.NewGuid();
hash[key] = key.ToByteArray();
// Guardando key para testar a leitura depois
if (i == HASH_SIZE / 2)
keyToRetrieve = key;
}
return hash;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment