Sha256: bc037d70c7e16cd8db0fe7faa863f21eb13325f82f66e4e531f0226de38b4878

Contents?: true

Size: 906 Bytes

Versions: 5

Compression:

Stored size: 906 Bytes

Contents

module JsDuck

  # Helper for timing execution of named code blocks.
  #
  #     timer = Timer.new
  #     a = timer.time(:sum) { 5 + 5 }
  #     b = timer.time(:sum) { 5 + 5 }
  #     c = timer.time(:mult) { 5 * 5 }
  #     d = timer.time(:mult) { 5 * 5 }
  #     timer.report
  #
  # The #report method will print sum of the time spent in each
  # category.
  #
  class Timer
    def initialize
      @timings = {}
    end

    # Performs timing of one code block.
    # Returns the same value that code block returns.
    def time(name)
      begin_time = Time.now
      result = yield
      interval = Time.now - begin_time
      if @timings[name]
        @timings[name] += interval
      else
        @timings[name] = interval
      end
      result
    end

    # prints timings report to console
    def report
      @timings.each {|name, time| puts "#{name}:\t#{time} seconds" }
    end
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
jsduck-0.6.1 lib/jsduck/timer.rb
jsduck-0.6 lib/jsduck/timer.rb
jsduck-0.5 lib/jsduck/timer.rb
jsduck-0.4 lib/jsduck/timer.rb
jsduck-0.3 lib/jsduck/timer.rb