Skip to content

Instantly share code, notes, and snippets.

@Dviejopomata Dviejopomata/cache.cs
Last active Nov 30, 2016

Embed
What would you like to do?
CACHE DE ACCESO DIRECTO Y LRU CACHE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cache
{
public class EntradaCache
{
public int bloque { get; set; }
public int tiempo { get; set; }
}
public class LruCache
{
public int tiempo { get; set; }
public List<EntradaCache> entrada;
public LruCache()
{
tiempo = 10;
entrada = new List<EntradaCache>()
{
new EntradaCache() {bloque=0, tiempo=0},
new EntradaCache() {bloque=1, tiempo=1},
new EntradaCache() {bloque=2, tiempo=2},
new EntradaCache() {bloque=3, tiempo=3}
};
}
public bool IfExiste(int bloque)
{
bool re = entrada.FirstOrDefault(i => i.bloque == bloque) == null ? false : true;
if (!re)
replace(bloque);
return re;
}
public void replace(int bloque)
{
tiempo++;
EntradaCache e = entrada.First(i => entrada.Min(j => j.tiempo) == i.tiempo);
e.bloque = bloque;
e.tiempo = tiempo;
}
}
class Program
{
static void Cache1()
{
Console.WriteLine("PRUEBA CACHE DE ACCESO DIRECTO" );
Dictionary<int, int> cache = new Dictionary<int, int>(){
{0,0},
{1,1},
{2,2},
{3,3}
};
List<int> lista = new List<int>
{
1, 2, 3, 4, 18, 24, 15, 54, 2, 3, 36, 55, 56, 26, 12, 33, 30, 58, 1, 32
};
int exitos = 0;
int fallos = 0;
lista.ForEach(i =>
{
int bloque = (i / 8);
int linea = bloque % 4;
bool exito = false;
int ant = cache[linea];
if (cache[linea] == bloque)
{
exitos++;
exito = true;
}
else
{
fallos++;
cache[linea] = bloque;
}
Console.WriteLine($"numero = {i} bloque= {bloque} linea={linea} {ant} exito={exito} {bloque * 8} {bloque * 8 + 7 }");
});
int total = exitos + fallos;
float porexito = ((float)exitos / (float)total) * 100;
float porfallos = (float)fallos / (float)total * 100;
float t = exitos * 2 + fallos * 20;
float media = t / lista.Count();
Console.WriteLine($"exitos = {exitos} fallos = {fallos} total = {total} porexistos={porexito} porfallo={porfallos} tiempomedia={media} ");
}
static void Lru()
{
Console.WriteLine("PRUEBA CON LRU CACHE");
LruCache cache = new LruCache();
List<int> lista = new List<int>
{
1, 2, 3, 4, 18, 24, 15, 54, 2, 3, 36, 55, 56, 26, 12, 33, 30, 58, 1, 32
};
int exitos = 0;
int fallos = 0;
lista.ForEach(i =>
{
int bloque = (i / 8);
bool exito = cache.IfExiste(bloque);
if (exito)
{
exitos++;
}
else
{
fallos++;
}
Console.WriteLine($"numero = {i} bloque= {bloque} exito={exito} {bloque * 8} {bloque * 8 + 7 }");
});
int total = exitos + fallos;
float porexito = ((float)exitos / (float)total) * 100;
float porfallos = (float)fallos / (float)total * 100;
float t = exitos * 2 + fallos * 20;
float media = t / lista.Count();
Console.WriteLine($"exitos = {exitos} fallos = {fallos} total = {total} porexistos={porexito} porfallo={porfallos} tiempomedia={media} ");
}
static void Main(string[] args)
{
Cache1();
Lru();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.