Class: Forthic::ProfilingState
- Inherits:
-
Object
- Object
- Forthic::ProfilingState
- Defined in:
- lib/forthic/interpreter.rb
Overview
Manages profiling state and operations
Instance Attribute Summary collapse
-
#is_profiling ⇒ Object
readonly
Returns the value of attribute is_profiling.
-
#start_profile_time ⇒ Object
readonly
Returns the value of attribute start_profile_time.
-
#timestamps ⇒ Object
readonly
Returns the value of attribute timestamps.
-
#word_counts ⇒ Object
readonly
Returns the value of attribute word_counts.
Instance Method Summary collapse
- #add_timestamp(label) ⇒ Object
- #count_word(word) ⇒ Object
-
#initialize ⇒ ProfilingState
constructor
A new instance of ProfilingState.
- #start_profiling ⇒ Object
- #stop_profiling ⇒ Object
- #word_histogram ⇒ Object
Constructor Details
#initialize ⇒ ProfilingState
Returns a new instance of ProfilingState.
61 62 63 64 65 66 |
# File 'lib/forthic/interpreter.rb', line 61 def initialize @word_counts = {} @is_profiling = false @start_profile_time = nil @timestamps = [] end |
Instance Attribute Details
#is_profiling ⇒ Object (readonly)
Returns the value of attribute is_profiling.
59 60 61 |
# File 'lib/forthic/interpreter.rb', line 59 def is_profiling @is_profiling end |
#start_profile_time ⇒ Object (readonly)
Returns the value of attribute start_profile_time.
59 60 61 |
# File 'lib/forthic/interpreter.rb', line 59 def start_profile_time @start_profile_time end |
#timestamps ⇒ Object (readonly)
Returns the value of attribute timestamps.
59 60 61 |
# File 'lib/forthic/interpreter.rb', line 59 def @timestamps end |
#word_counts ⇒ Object (readonly)
Returns the value of attribute word_counts.
59 60 61 |
# File 'lib/forthic/interpreter.rb', line 59 def word_counts @word_counts end |
Instance Method Details
#add_timestamp(label) ⇒ Object
87 88 89 90 91 |
# File 'lib/forthic/interpreter.rb', line 87 def (label) return unless @is_profiling = {label: label, time_ms: (Time.now - @start_profile_time) * 1000} @timestamps.push() end |
#count_word(word) ⇒ Object
81 82 83 84 85 |
# File 'lib/forthic/interpreter.rb', line 81 def count_word(word) return unless @is_profiling @word_counts[word.name] ||= 0 @word_counts[word.name] += 1 end |
#start_profiling ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/forthic/interpreter.rb', line 68 def start_profiling @is_profiling = true @timestamps = [] @start_profile_time = Time.now ("START") @word_counts = {} end |
#stop_profiling ⇒ Object
76 77 78 79 |
# File 'lib/forthic/interpreter.rb', line 76 def stop_profiling ("END") @is_profiling = false end |
#word_histogram ⇒ Object
93 94 95 |
# File 'lib/forthic/interpreter.rb', line 93 def word_histogram @word_counts.map { |name, count| {word: name, count: count} }.sort_by { |item| -item[:count] } end |