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.
451 |
# File 'lib/im-lost.rb', line 451 def count = ids.size |
#empty? ⇒ Boolean (readonly)
Returns whether the timer store is empty or not.
455 |
# File 'lib/im-lost.rb', line 455 def empty? = ids.empty? |
#ids ⇒ Array<Integer> (readonly)
Returns IDs of all registered timers.
459 |
# File 'lib/im-lost.rb', line 459 def ids = @ll.keys.keep_if { Integer === _1 } |
#names ⇒ Array<String> (readonly)
Returns names of all registered named timers.
463 |
# File 'lib/im-lost.rb', line 463 def names = @ll.keys.delete_if { Integer === _1 } |
Class Method Details
.now ⇒ Float
Returns current time.
443 |
# File 'lib/im-lost.rb', line 443 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.
519 520 521 522 523 524 525 |
# File 'lib/im-lost.rb', line 519 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.
533 534 535 536 537 538 |
# File 'lib/im-lost.rb', line 533 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.
505 506 507 508 |
# File 'lib/im-lost.rb', line 505 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.
472 473 474 475 476 477 478 479 |
# File 'lib/im-lost.rb', line 472 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.
487 488 489 490 491 492 493 494 495 496 497 498 |
# File 'lib/im-lost.rb', line 487 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 |