OOP vs DOD 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
static string buffer = "";
static int cacheMisses = 0;
const int numberOfValuesCached = 4;
const int maxNumberOf32BitsElements = 16;
class MemoryThing
{
}
class Float : MemoryThing
{
private readonly float value;
public Float(float value)
{
this.value = value;
}
public static implicit operator Float(float value)
{
return new Float(value);
}
public static Float operator +(Float first, Float second)
{
return new Float(first.value + second.value);
}
public static Float operator -(Float first, Float second)
{
return new Float(first.value - second.value);
}
public override string ToString()
{
return "{" + value + "}";
}
}
class Int : MemoryThing
{
private readonly int value;
public Int(int value)
{
this.value = value;
}
public static implicit operator Int(int value)
{
return new Int(value);
}
public static Int operator +(Int first, Int second)
{
return new Int(first.value + second.value);
}
public static Int operator -(Int first, Int second)
{
return new Int(first.value - second.value);
}
public override string ToString()
{
return "{" + value + "}";
}
}
class Entity : MemoryThing
{
public Float x, y;
public Int sprite;
public Entity(float x, float y, int sprite)
{
this.x = x;
this.y = y;
this.sprite = sprite;
}
public override string ToString()
{
return x.ToString() + y.ToString() + sprite.ToString();
}
}
class Position : MemoryThing
{
public Float x, y;
public Position(float x, float y)
{
this.x = x;
this.y = y;
}
public override string ToString()
{
return x.ToString() + y.ToString();
}
}
class Entities
{
public List<MemoryThing> positions = new List<MemoryThing>();
public List<MemoryThing> sprites = new List<MemoryThing>();
public void NewEntity(float x, float y, int sprite)
{
positions.Add(new Position(x,y));
sprites.Add(new Int(sprite));
}
}
static void StoreValueInMemory(List<MemoryThing> objects, int pos)
{
int cachedValues = 0;
for(int i = pos; i < objects.Count; ++i)
{
string values = objects[i].ToString();
int numValues = values.Count(f => f == '{');
while(cachedValues < numberOfValuesCached)
{
int indexToCopyTo = values.IndexOf('}');
if(indexToCopyTo >= 0)
{
string newValue = values.Substring(0, indexToCopyTo + 1);
if(buffer.IndexOf(newValue) < 0)
{
buffer += newValue;
cachedValues++;
}
values = values.Substring(indexToCopyTo + 1);
}
else
{
break;
}
}
if(cachedValues >= numberOfValuesCached)
{
break;
}
}
int count = buffer.Count(f => f == '{');
while(count > maxNumberOf32BitsElements)
{
int indexToDeleteTo = buffer.IndexOf('}');
if(indexToDeleteTo >= 0)
{
buffer = buffer.Substring(indexToDeleteTo + 1);
count--;
}
else
{
break;
}
}
Console.WriteLine(string.Format("Stored in memory : {0}", buffer));
}
static void GetValueInMemory(List<MemoryThing> objects, int pos)
{
MemoryThing yourObject = objects[pos];
string values = yourObject.ToString();
int numValues = values.Count(f => f == '{');
for(int i = 0; i < numValues; ++i)
{
int indexToCopyTo = values.IndexOf('}');
if(indexToCopyTo >= 0)
{
string newValue = values.Substring(0, indexToCopyTo + 1);
if(buffer.IndexOf(newValue) != -1)
{
values = values.Substring(indexToCopyTo + 1);
}
else
{
cacheMisses++;
StoreValueInMemory(objects, pos);
}
}
else
{
break;
}
}
}
public static void Main(string[] args)
{
int numberOfEntities = 10;
List<MemoryThing> entities = new List<MemoryThing>();
for(int i = 0; i < numberOfEntities; ++i)
{
entities.Add(new Entity((float)i + 10000.0f , (float)i + 200000.0f, i));
}
for(int i = 0; i < numberOfEntities; ++i)
{
GetValueInMemory(entities, i);
}
for(int i = 0; i < numberOfEntities; ++i)
{
GetValueInMemory(entities, i);
}
Console.WriteLine(string.Format("Number of cache misses for OOP: {0}", cacheMisses));
cacheMisses = 0;
buffer = "";
Entities DoDEntities = new Entities();
for(int i = 0; i < numberOfEntities; ++i)
{
DoDEntities.NewEntity((float)i + 10000.0f , (float)i + 200000.0f, i);
}
for(int i = 0; i < numberOfEntities; ++i)
{
GetValueInMemory(DoDEntities.positions, i);
}
for(int i = 0; i < numberOfEntities; ++i)
{
GetValueInMemory(DoDEntities.sprites, i);
}
Console.WriteLine(string.Format("Number of cache misses for DOD: {0}", cacheMisses));
}
}
}
|
run
| edit
| history
| help
|
0
|
|
|