Sha256: 992ca1dc339d0fc9ab8925a86ac8a02f5cf4c750fd51fc98d4a13f010be72266

Contents?: true

Size: 961 Bytes

Versions: 5

Compression:

Stored size: 961 Bytes

Contents

require 'jsduck/logger'

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 log
    def report
      @timings.each do |name, time|
        Logger.instance.log("#{name}:\t#{time} seconds")
      end
    end
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
jsduck-3.0.pre2 lib/jsduck/timer.rb
jsduck-3.0.pre lib/jsduck/timer.rb
jsduck-2.0.pre4 lib/jsduck/timer.rb
jsduck-2.0.pre2 lib/jsduck/timer.rb
jsduck-2.0.pre lib/jsduck/timer.rb