|  | 
|  | 1 | +using Arch.Core; | 
|  | 2 | +using Arch.Core.Utils; | 
|  | 3 | + | 
|  | 4 | +namespace Arch.Benchmarks; | 
|  | 5 | + | 
|  | 6 | +[HtmlExporter] | 
|  | 7 | +//[MemoryDiagnoser] | 
|  | 8 | +//[HardwareCounters(HardwareCounter.CacheMisses)] | 
|  | 9 | +public class DuplicateBenchmark | 
|  | 10 | +{ | 
|  | 11 | +    public int Amount = 100000; | 
|  | 12 | + | 
|  | 13 | +    private static readonly ComponentType[] _group = { typeof(Transform), typeof(Velocity) }; | 
|  | 14 | +    private readonly QueryDescription _queryDescription = new(all: _group); | 
|  | 15 | + | 
|  | 16 | +    private static World? _world; | 
|  | 17 | +    private static Entity _entity = Entity.Null; | 
|  | 18 | +    private static Entity[]? _array = null; | 
|  | 19 | + | 
|  | 20 | +    [IterationSetup] | 
|  | 21 | +    public void Setup() | 
|  | 22 | +    { | 
|  | 23 | +        _world = World.Create(); | 
|  | 24 | +        _world.Reserve(_group, 1); | 
|  | 25 | +        _entity = _world.Create(new Transform { X = 111, Y = 222}, new Velocity { X = 333, Y = 444 }); | 
|  | 26 | +        _array = new Entity[Amount]; | 
|  | 27 | +    } | 
|  | 28 | + | 
|  | 29 | +    [IterationCleanup] | 
|  | 30 | +    public void Cleanup() | 
|  | 31 | +    { | 
|  | 32 | +        World.Destroy(_world); | 
|  | 33 | +        _world = null; | 
|  | 34 | +    } | 
|  | 35 | + | 
|  | 36 | +    /// DuplicateN() method. | 
|  | 37 | +    [Benchmark] | 
|  | 38 | +    public void DuplicateNInternal() | 
|  | 39 | +    { | 
|  | 40 | +        _world.DuplicateN(_entity, _array.AsSpan()); | 
|  | 41 | +    } | 
|  | 42 | + | 
|  | 43 | +    /// DuplicateN() in terms of Duplicate() method. | 
|  | 44 | +    [Benchmark] | 
|  | 45 | +    public void DuplicateNDuplicate() | 
|  | 46 | +    { | 
|  | 47 | +        for (int i = 0; i < Amount; ++i) | 
|  | 48 | +        { | 
|  | 49 | +            _array[i] = _world.Duplicate(_entity); | 
|  | 50 | +        } | 
|  | 51 | +    } | 
|  | 52 | + | 
|  | 53 | +    /// Benchmark DuplicateN() if implemented via GetAllComponents. | 
|  | 54 | +    [Benchmark] | 
|  | 55 | +    public void DuplicateNGetAllComponents() | 
|  | 56 | +    { | 
|  | 57 | +        for (int i = 0; i < Amount; ++i) | 
|  | 58 | +        { | 
|  | 59 | +            var arch = _world.GetArchetype(_entity); | 
|  | 60 | +            var copiedEntity = _world.Create(arch.Signature); | 
|  | 61 | +            foreach (var c in _world.GetAllComponents(_entity)) | 
|  | 62 | +            { | 
|  | 63 | +                _world.Set(_entity, c); | 
|  | 64 | +            } | 
|  | 65 | +        } | 
|  | 66 | +    } | 
|  | 67 | +} | 
0 commit comments