forked from guitsaru/draco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.rb
84 lines (67 loc) · 1.78 KB
/
benchmark.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# frozen_string_literal: true
require_relative "lib/draco"
require "benchmark"
puts "Defining Component"
class SampleComponent < Draco::Component
attribute :a, default: 0
attribute :b, default: 0
attribute :c, default: 0
attribute :d, default: 0
attribute :e, default: 0
attribute :f, default: 0
attribute :g, default: 0
end
puts "Defining System"
class SampleSystem < Draco::System
filter SampleComponent
def tick(_)
entities.each do |entity|
entity.sample_component.a
end
end
end
puts "Dynamic Components"
dynamic_components = (1..1000).map do |i|
name = "DynamicComponent#{i}"
klass = Class.new(SampleComponent)
Object.const_set(name, klass)
klass
end
puts "Defining Entity"
class SampleEntity < Draco::Entity
component SampleComponent
end
puts "Adding dynamic components to Entity"
dynamic_components.each do |dynamic_component|
SampleEntity.component(dynamic_component) if [true, false, false, false, false].sample
end
puts "Defining World"
world = Draco::World.new
world.systems << SampleSystem
Benchmark.bm do |bm|
bm.report("initialize entity") do
SampleEntity.new
end
end
puts "Generating 10_000 entities"
(1..10_000).each do |i|
print "#{i} " if (i % 100).zero?
entity = SampleEntity.new
world.entities << entity
end
puts
puts "Goal: #{1.0 / 60.0}"
Benchmark.bm do |bm|
bm.report("tick") do
world.tick(nil)
end
bm.report("filter") { world.filter(SampleComponent) }
end
# Initial Implementation
# user system total real
# tick 0.345672 0.006669 0.352341 ( 0.354792)
# filter 0.288709 0.000000 0.288709 ( 0.290659)
# After optimization
# user system total real
# tick 0.007575 0.000005 0.007580 ( 0.007589)
# filter 0.000013 0.000000 0.000013 ( 0.000012)