-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
178 additions
and
14 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using fennecs; | ||
|
||
namespace Benchmark.Conceptual; | ||
|
||
public class CostCentersBench | ||
{ | ||
[Params(10000)] | ||
public int Count { get; set; } | ||
|
||
private World _world = null!; | ||
private Stream<int> _streamOne = null!; | ||
private Stream<int> _streamMany = null!; | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
_world = new(Count * 3); | ||
|
||
for (var i = 0; i < Count; i++) | ||
{ | ||
_world.Spawn().Add(i); | ||
} | ||
|
||
for (var i = 0; i < Count; i++) | ||
{ | ||
var unique = _world.Spawn(); | ||
_world.Spawn().Add(i).Add("relation", unique); | ||
} | ||
|
||
_streamOne = _world.Query<int>().Not<string>(Match.Any).Stream(); | ||
_streamMany = _world.Query<int>().Has<string>(Match.Any).Stream(); | ||
|
||
Console.WriteLine($"World: {_world.Count} Entities"); | ||
Console.WriteLine($"Stream One: {_streamOne.Count} Entities, {_streamOne.Query.Archetypes.Count} Archetypes"); | ||
Console.WriteLine($"Stream Many: {_streamMany.Count} Entities, {_streamMany.Query.Archetypes.Count} Archetypes"); | ||
} | ||
|
||
[Benchmark] | ||
public int SingleArchetype() | ||
{ | ||
var output = 0; | ||
_streamOne.For((ref int value) => { output += value; }); | ||
return output; | ||
} | ||
|
||
[Benchmark] | ||
public int ManyArchetypes() | ||
{ | ||
var output = 0; | ||
_streamMany.For((ref int value) => { output += value; }); | ||
return output; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace Benchmark.Conceptual; | ||
|
||
public class EventsVsListsBench | ||
{ | ||
[Params(100)] | ||
public int Count { get; set; } | ||
|
||
private List<Action<int>> _listeners = null!; | ||
private event Action<int>? Event = null!; | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
_listeners = new(1_000_000) | ||
{ | ||
OnEvent, | ||
}; | ||
Event += OnEvent; | ||
} | ||
|
||
private int _sum; | ||
private Random _rnd = null!; | ||
private void OnEvent(int number) | ||
{ | ||
_sum += number; | ||
} | ||
|
||
[IterationSetup] | ||
public void SetupEvents() | ||
{ | ||
_sum = 0; | ||
_rnd = new(); | ||
|
||
_listeners.Clear(); | ||
Event = null!; | ||
|
||
for (var i = 0; i < Count; i++) | ||
{ | ||
_listeners.Add(OnEvent); | ||
Event += OnEvent; | ||
} | ||
} | ||
|
||
|
||
[Benchmark] | ||
public void AddListeners() | ||
{ | ||
for (var i = 0; i < 10_000; i++) | ||
{ | ||
_listeners.Add(OnEvent); | ||
} | ||
|
||
for (var i = 0; i < 10_000; i++) | ||
{ | ||
_listeners.Remove(OnEvent); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void AddEvents() | ||
{ | ||
for (var i = 0; i < 10_000; i++) | ||
{ | ||
Event += OnEvent; | ||
} | ||
|
||
for (var i = 0; i < 10_000; i++) | ||
{ | ||
Event -= OnEvent; | ||
} | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public int InvokeEvents() | ||
{ | ||
for (var i = 0; i < 1_000_000; i++) | ||
{ | ||
var number = _rnd.Next(); | ||
Event?.Invoke(number); | ||
} | ||
return _sum; | ||
} | ||
|
||
[Benchmark] | ||
public int InvokeListeners() | ||
{ | ||
for (var i = 0; i < 1_000_000; i++) | ||
{ | ||
var number = _rnd.Next(); | ||
foreach (var action in _listeners) action(number); | ||
} | ||
return _sum; | ||
} | ||
} |
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
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
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