Created
November 25, 2011 17:04
-
-
Save bernardobrezende/1393980 to your computer and use it in GitHub Desktop.
Microbenchmark para escrita/leitura em hashtable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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