Last active
December 24, 2020 06:20
-
-
Save AbePralle/162c8d33dd5239b1d0aa4ad15e7a1047 to your computer and use it in GitHub Desktop.
Testing tree-based vs component-based attributes
This file contains hidden or 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
| uses Utility/Node | |
| class ComponentObject | |
| PROPERTIES | |
| attributes : ComponentAttribute | |
| METHODS | |
| method init | |
| method init( attribute:ComponentAttribute ) | |
| add( attribute ) | |
| method add( attribute:ComponentAttribute ) | |
| if (attributes) attributes.add_next( attribute ) | |
| else attributes = attribute | |
| method color->Int32 | |
| if (attributes) return attributes.color | |
| return 0 | |
| method shape->Int32 | |
| if (attributes) return attributes.shape | |
| return 0 | |
| endClass | |
| class ComponentAttribute : Node<<ComponentAttribute>> | |
| METHODS | |
| method color->Int32 | |
| if (next) return next.color | |
| return 0 | |
| method shape->Int32 | |
| if (next) return next.shape | |
| return 0 | |
| endClass | |
| class ComponentColor : ComponentAttribute | |
| PROPERTIES | |
| color = Random.int32(100) : Int32 | |
| METHODS | |
| method color->Int32 | |
| return @color | |
| endClass | |
| class ComponentShape : ComponentAttribute | |
| PROPERTIES | |
| shape = Random.int32(100) : Int32 | |
| METHODS | |
| method shape->Int32 | |
| return @shape | |
| endClass | |
| routine test_component | |
| local objects = ComponentObject[]( 1000 ) | |
| loop objects.capacity | |
| local obj = ComponentObject() | |
| if (Random.logical) obj.add( ComponentColor() ) | |
| if (Random.logical) obj.add( ComponentShape() ) | |
| objects.add( obj ) | |
| endLoop | |
| local stopwatch = Stopwatch() | |
| local avg_color = 0 | |
| local avg_shape = 0 | |
| loop 10000 | |
| forEach (object in objects) | |
| avg_color += object.color | |
| avg_shape += object.shape | |
| endForEach | |
| endLoop | |
| trace stopwatch | |
| endRoutine | |
| test_component | |
| test_component | |
| test_component | |
| # RESULTS | |
| stopwatch:0.30 seconds | |
| stopwatch:0.30 seconds | |
| stopwatch:0.31 seconds |
This file contains hidden or 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
| uses Utility/Node | |
| class TreeObject : Node<<TreeObject>> | |
| METHODS | |
| method init | |
| method init( child:TreeObject ) | |
| add( child ) | |
| method color->Int32 | |
| return 0 | |
| method drawable->TreeObject | |
| local cur = this | |
| while (cur.first_child) cur .= first_child | |
| return cur | |
| method root->TreeObject | |
| local cur = this | |
| while (cur.parent) cur .= parent | |
| return cur | |
| method shape->Int32 | |
| return 0 | |
| endClass | |
| class TreeAttribute : TreeObject | |
| METHODS | |
| method color->Int32 | |
| if (first_child) return first_child.color | |
| return 0 | |
| method shape->Int32 | |
| if (first_child) return first_child.shape | |
| return 0 | |
| endClass | |
| class TreeColor : TreeAttribute | |
| PROPERTIES | |
| color = Random.int32(100) : Int32 | |
| METHODS | |
| method color->Int32 | |
| return @color | |
| endClass | |
| class TreeShape : TreeAttribute | |
| PROPERTIES | |
| shape = Random.int32(100) : Int32 | |
| METHODS | |
| method shape->Int32 | |
| return @shape | |
| endClass | |
| routine test_tree | |
| local objects = TreeObject[]( 1000 ) | |
| loop objects.capacity | |
| local obj = TreeObject() | |
| if (Random.logical) obj = TreeColor( obj ) | |
| if (Random.logical) obj = TreeShape( obj ) | |
| objects.add( obj ) | |
| endLoop | |
| local stopwatch = Stopwatch() | |
| local avg_color = 0 | |
| local avg_shape = 0 | |
| loop 10000 | |
| forEach (object in objects) | |
| # In real use the actual drawable leaf would be doing the drawing and | |
| # need to use 'root' to get back to the top of the attribute subtree, | |
| # so we'll simulate that here by getting the drawable leaf and then | |
| # backing up to the root. | |
| local root = object.drawable.root | |
| avg_color += root.color | |
| avg_shape += root.shape | |
| endForEach | |
| endLoop | |
| @trace stopwatch | |
| endRoutine | |
| test_tree | |
| test_tree | |
| test_tree | |
| # RESULTS | |
| stopwatch:0.18 seconds | |
| stopwatch:0.18 seconds | |
| stopwatch:0.18 seconds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment