Class: ImLost::TimerStore
- Inherits:
-
Object
- Object
- ImLost::TimerStore
- Defined in:
- lib/im-lost.rb
Overview
A store to create and register timers you can use to estimate the runtime of some code.
All timers are identified by an unique ID or a name.
Instance Attribute Summary collapse
-
#count ⇒ Integer
readonly
The number of registered timers.
-
#empty? ⇒ Boolean
readonly
Whether the timer store is empty or not.
-
#ids ⇒ Array<Integer>
readonly
IDs of all registered timers.
-
#names ⇒ Array<String>
readonly
Names of all registered named timers.
Class Method Summary collapse
-
.now ⇒ Float
Current time.
Instance Method Summary collapse
-
#[](id_or_name) ⇒ Integer
Print the ID or name and the runtime since a timer was created.
-
#all ⇒ nil
Print the ID or name and the runtime of all active timers.
-
#clear ⇒ self
Delete and unregister all timers.
-
#create(name = nil) ⇒ Integer
Create and register a new named or anonymous timer.
-
#delete(*id_or_names) ⇒ nil
Delete and unregister timers.
Instance Attribute Details
#count ⇒ Integer (readonly)
Returns the number of registered timers.
473 |
# File 'lib/im-lost.rb', line 473 def count = ids.size |
#empty? ⇒ Boolean (readonly)
Returns whether the timer store is empty or not.
477 |
# File 'lib/im-lost.rb', line 477 def empty? = ids.empty? |
#ids ⇒ Array<Integer> (readonly)
Returns IDs of all registered timers.
481 |
# File 'lib/im-lost.rb', line 481 def ids = @ll.keys.keep_if { Integer === _1 } |
#names ⇒ Array<String> (readonly)
Returns names of all registered named timers.
485 |
# File 'lib/im-lost.rb', line 485 def names = @ll.keys.delete_if { Integer === _1 } |
Class Method Details
.now ⇒ Float
Returns current time.
465 |
# File 'lib/im-lost.rb', line 465 def self.now = Process.clock_gettime(Process::CLOCK_MONOTONIC) |
Instance Method Details
#[](id_or_name) ⇒ Integer
Print the ID or name and the runtime since a timer was created. It includes the location.
541 542 543 544 545 546 547 |
# File 'lib/im-lost.rb', line 541 def [](id_or_name) now = self.class.now timer = @ll[Integer === id_or_name ? id_or_name : id_or_name.to_s] raise(ArgumentError, "not a timer - #{id_or_name.inspect}") unless timer @cb[timer[0], Kernel.caller_locations(1, 1)[0], (now - timer[1]).round(4)] timer.__id__ end |
#all ⇒ nil
Print the ID or name and the runtime of all active timers. It includes the location.
555 556 557 558 559 560 |
# File 'lib/im-lost.rb', line 555 def all now = self.class.now loc = Kernel.caller_locations(1, 1)[0] @ll.values.uniq.reverse_each { |name, start| @cb[name, loc, now - start] } nil end |
#clear ⇒ self
Delete and unregister all timers.
527 528 529 530 |
# File 'lib/im-lost.rb', line 527 def clear @ll = {} self end |
#create(name = nil) ⇒ Integer
Create and register a new named or anonymous timer. It print the ID or name of the created timer and includes the location.
494 495 496 497 498 499 500 501 |
# File 'lib/im-lost.rb', line 494 def create(name = nil) timer = [] @ll[id = timer.__id__] = timer name ? @ll[name = name.to_s] = timer : name = id @cb[name, Kernel.caller_locations(1, 1)[0]] timer << name << self.class.now id end |
#delete(*id_or_names) ⇒ nil
Delete and unregister timers.
509 510 511 512 513 514 515 516 517 518 519 520 |
# File 'lib/im-lost.rb', line 509 def delete(*id_or_names) id_or_names.flatten.each do |id| if Integer === id del = @ll.delete(id) @ll.delete(del[0]) if del else del = @ll.delete(id.to_s) @ll.delete(del.__id__) if del end end nil end |