Skip to content

Instantly share code, notes, and snippets.

@SH42913
Last active January 16, 2024 16:28
Show Gist options
  • Save SH42913/021af9324e4afd2149c372e882fa0756 to your computer and use it in GitHub Desktop.
Save SH42913/021af9324e4afd2149c372e882fa0756 to your computer and use it in GitHub Desktop.
Benchmarking of in modifier in C#
private const int CallDepth = 300;
public static class MorpehExt
{
public static float DoGetterExt(this in MorpehBaseContext.Component3 c3) => c3.Value1 * c3.Value2;
}
public struct Component1
{
public int Value;
}
public struct Component2
{
public int Value;
public float Value1;
}
public struct Component3
{
public int Value;
public float Value1;
public float Value2;
public float Getter => Value1 * Value2;
public float GetterPure
{
[Pure]
get { return Value1 * Value2; }
}
public float GetterChange
{
get
{
Value++;
return Value1 * Value2;
}
}
public float DoGetter()
{
return Value1 * Value2;
}
}
[Benchmark]
public void No_In()
{
var comp2 = new Component2
{
Value = 1,
Value1 = 1,
};
var comp3 = new Component3
{
Value = 1,
Value1 = 1,
Value2 = 1,
};
Test(comp2, comp3, CallDepth).Equals(1f);
float Test(Component2 c1, Component3 c2, int depth)
{
return depth > 0
? Test(c1, c2, depth - 1) + c2.Value1 * c2.Value2
: c1.Value * c2.Value + c2.Value1 * c2.Value2;
}
}
[Benchmark]
public void With_In()
{
var comp2 = new Component2
{
Value = 1,
Value1 = 1,
};
var comp3 = new Component3
{
Value = 1,
Value1 = 1,
Value2 = 1,
};
Test(comp2, comp3, CallDepth).Equals(1f);
float Test(in Component2 c1, in Component3 c2, int depth)
{
return depth > 0
? Test(c1, c2, depth - 1) + c2.Value1 * c2.Value2
: c1.Value * c2.Value + c2.Value1 * c2.Value2;
}
}
[Benchmark]
public void With_In_Getter()
{
var comp2 = new Component2
{
Value = 1,
Value1 = 1,
};
var comp3 = new Component3
{
Value = 1,
Value1 = 1,
Value2 = 1,
};
Test(comp2, comp3, CallDepth).Equals(1f);
float Test(in Component2 c1, in Component3 c2, int depth)
{
return depth > 0
? Test(c1, c2, depth - 1) + c2.Getter
: c1.Value * c2.Value + c2.Getter;
}
}
[Benchmark]
public void With_In_Getter_Pure()
{
var comp2 = new Component2
{
Value = 1,
Value1 = 1,
};
var comp3 = new Component3
{
Value = 1,
Value1 = 1,
Value2 = 1,
};
Test(comp2, comp3, CallDepth).Equals(1f);
float Test(in Component2 c1, in Component3 c2, int depth)
{
return depth > 0
? Test(c1, c2, depth - 1) + c2.GetterPure
: c1.Value * c2.Value + c2.GetterPure;
}
}
[Benchmark]
public void With_In_Getter_Impure()
{
var comp2 = new Component2
{
Value = 1,
Value1 = 1,
};
var comp3 = new Component3
{
Value = 1,
Value1 = 1,
Value2 = 1,
};
Test(comp2, comp3, CallDepth).Equals(1f);
float Test(in Component2 c1, in Component3 c2, int depth)
{
return depth > 0
? Test(c1, c2, depth - 1) + c2.GetterChange
: c1.Value * c2.Value + c2.GetterChange;
}
}
[Benchmark]
public void With_In_Method()
{
var comp2 = new Component2
{
Value = 1,
Value1 = 1,
};
var comp3 = new Component3
{
Value = 1,
Value1 = 1,
Value2 = 1,
};
Test(comp2, comp3, CallDepth).Equals(1f);
float Test(in Component2 c1, in Component3 c2, int depth)
{
return depth > 0
? Test(c1, c2, depth - 1) + c2.DoGetter()
: c1.Value * c2.Value + c2.DoGetter();
}
}
[Benchmark]
public void With_In_ExtMethod()
{
var comp2 = new Component2
{
Value = 1,
Value1 = 1,
};
var comp3 = new Component3
{
Value = 1,
Value1 = 1,
Value2 = 1,
};
Test(comp2, comp3, CallDepth).Equals(1f);
float Test(in Component2 c1, in Component3 c2, int depth)
{
return depth > 0
? Test(c1, c2, depth - 1) + c2.DoGetterExt()
: c1.Value * c2.Value + c2.DoGetterExt();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment