I made 2 benchmarks, one for my oop class
class oop {
public float[] position = new float[2];
public float[] scale = new float[2];
public float[] rotation = new float[2];
public string name;
public int weight;
public void setPosition() {
position[0] = 1.5f;
position[1] = 3f;
}
public void setScale() {
scale[0] = 1.5f;
scale[1] = 3f;
}
public void setRotation() {
rotation[0] = 1.5f;
rotation[1] = 3f;
}
public void setName() {
name = "Gregor";
}
public void setWeight() {
weight = 150;
}
}
and one for my entity class
class entity {
// Entity properties
public position position;
public rotation rotation;
public scale scale;
public name name;
public weight weight;
}
each property in the entity is a struct.
I tried to benchmark them:
for (long i = 0; i < times; i++) {
oop test = new oop();
test.setPosition();
test.setRotation();
test.setScale();
test.setName();
test.setWeight();
}
for (long i = 0; i < times; i++) {
entity e = new entity();
e.position.x = 1.5f;
e.position.y = 3f;
e.rotation.x = 1.5f;
e.rotation.y = 3f;
e.scale.x = 1.5f;
e.scale.y = 3f;
e.name.n = "Gregor";
e.weight.w = 150;
}
my results:
OOP 0ms (1)
DoD 0ms (1)
OOP 0ms (10)
DoD 0ms (10)
OOP 0ms (100)
DoD 0ms (100)
OOP 0ms (1000)
DoD 0ms (1000)
OOP 4ms (10000)
DoD 0ms (10000)
OOP 9ms (100000)
DoD 5ms (100000)
OOP 78ms (1000000)
DoD 42ms (1000000)
OOP 786ms (10000000)
DoD 539ms (10000000)
OOP 6455ms (100000000)
DoD 4107ms (100000000)
My question is: Why is this possible? In my dod class i have the same amount of values as in my oop class, and i set the same values for everything. The float arrays are slower than the structs? I wanted to make an a Data Oriented Design, but its not great like that, but the result is a lot better than my oop class
entitycode doesn't compile. I assume you meant to use the same types as theoopexample?xandyproperties. Can you please post a minimal reproducible example so that we can run the benchmarks ourselves?