Class: Forthic::ProfilingState

Inherits:
Object
  • Object
show all
Defined in:
lib/forthic/interpreter.rb

Overview

Manages profiling state and operations

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProfilingState

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_profilingObject (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_timeObject (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

#timestampsObject (readonly)

Returns the value of attribute timestamps.



59
60
61
# File 'lib/forthic/interpreter.rb', line 59

def timestamps
  @timestamps
end

#word_countsObject (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 add_timestamp(label)
  return unless @is_profiling
  timestamp = {label: label, time_ms: (Time.now - @start_profile_time) * 1000}
  @timestamps.push(timestamp)
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_profilingObject



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
  add_timestamp("START")
  @word_counts = {}
end

#stop_profilingObject



76
77
78
79
# File 'lib/forthic/interpreter.rb', line 76

def stop_profiling
  add_timestamp("END")
  @is_profiling = false
end

#word_histogramObject



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