注册会员 登录
Project1 返回首页

QQEat https://rpg.blue/?326184 [收藏] [复制] [分享] [RSS] 这个人很懒,什么也没留下。

日志

【RM】Benchmark v1.0 脚本基准测试

已有 65 次阅读2023-1-15 11:40 |个人分类:脚本| Benchmark

  1. #encoding:utf-8
  2. #==============================================================================
  3. # ■ Benchmark v1.0 By QQEat
  4. #------------------------------------------------------------------------------
  5. #  基准测试(优雅 + 完美强迫症打印)
  6. #==============================================================================
  7. #
  8. #  # 打印代码块执行时间(count 默认 1, 可在下面设置更改)
  9. #  Benchmark.measure(count){|r| ... }
  10. #
  11. #  # 测试代码快效率
  12. #  Benchmark.measure(10_000) { [*1..1000].shuffle }
  13. #
  14. #  # 汇报打印
  15. #  Benchmark.measure do |x|
  16. #    a = (1..100).to_a.shuffle!
  17. #    x.report('Array#sort', 100000)  { a.sort  }
  18. #    x.report('Array#sort!', 100000) { a.sort! }
  19. #    10.times{ x.report{ a.sort! } }
  20. #  end
  21. #

  22. module Benchmark
  23.   #--------------------------------------------------------------------------
  24.   # ● 常量
  25.   #--------------------------------------------------------------------------
  26.   ITERATIONS = 1 # 默认执行次数
  27.   MARGIN = 3 # 列间距
  28.   SPLIT_LINE = true # 是否显示列分割线
  29.   COLS = [
  30.     [7, '#', :R],
  31.     [10, 'user', :R],   # 用户时间
  32.     [10, 'system', :R], # 系统时间
  33.     [10, 'real', :R],   # 真实用时
  34.     [10, 'avg', :R],    # 平均用时
  35.     [10, 'times', :R],  # 执行次数
  36.   ].map.with_index{|v, i|
  37.     [v[0], v[1].send(v[2] == :L ? :ljust : :rjust, v[0]) + ' ' * MARGIN, v[2]]
  38.   } # [列宽, 列名, 对齐方向]
  39.   
  40.   #==============================================================================
  41.   # ■ Reporter
  42.   #==============================================================================
  43.   class Reporter
  44.     #--------------------------------------------------------------------------
  45.     # ● 初始化
  46.     #--------------------------------------------------------------------------
  47.     def initialize
  48.       @total = [0, 0, 0, 0]
  49.     end
  50.     #--------------------------------------------------------------------------
  51.     # ● 汇报执行(标签名, 执行次数)
  52.     #--------------------------------------------------------------------------
  53.     def report(label = nil, iterate = ITERATIONS)
  54.       result = Benchmark.send(:time, iterate) { yield }
  55.       result << result[-1] / iterate
  56.       Benchmark.send(:output, label, *result.map{|v| '%0.6f' % v }, iterate)
  57.       @total.map!.with_index { |t, i| t += result[i].to_f }
  58.     end
  59.   end
  60.   #--------------------------------------------------------------------------
  61.   # ● measure 测量脚本执行时间
  62.   #--------------------------------------------------------------------------
  63.   def self.measure(iterate = ITERATIONS, &block)
  64.     @row = 0
  65.     width = COLS.inject(0){|a,b| a+b[1].length } / 2 - 5
  66.     puts "#{'-'*width} BENCHMARK #{'-'*width}"
  67.     puts COLS.map{|v| v[1] }.join
  68.     reporter = Reporter.new
  69.     if block.arity == 0
  70.       result = time(iterate) { yield }
  71.     else
  72.       yield reporter
  73.       result = reporter.instance_variable_get(:@total)
  74.     end
  75.     print "\n"
  76.     output('Total', *result.map{|v| '%0.6f' % v }, '')
  77.     print "\n"
  78.   end
  79.   
  80.   class << self
  81.     private
  82.     #--------------------------------------------------------------------------
  83.     # ● 执行脚本(执行次数)
  84.     #   return:[用户时间, 系统时间, 真实时间]
  85.     #--------------------------------------------------------------------------
  86.     def time(iterate = ITERATIONS)
  87.       t = Process.times
  88.       initial = [t.utime, t.stime, Time.now]
  89.       iterate.times { yield }
  90.       t2 = Process.times
  91.       [t2.utime, t2.stime, Time.now].map!.with_index do |time, index|
  92.         time - initial[index]
  93.       end
  94.     end
  95.     #--------------------------------------------------------------------------
  96.     # ● 打印输出
  97.     #--------------------------------------------------------------------------
  98.     def output(*args)
  99.       args[-1] = args[-1].to_s.gsub(/(?=(?!\b)(\d{3})+$)/, ',')
  100.       (args[0] ||= @row.to_s) << ':'
  101.       args.map!.with_index{|v,i| v.to_s.scan(/.{1,#{COLS[i][0]}}/) }
  102.       row = args.max_by{|v| v.length }.length
  103.       row.times do |i|
  104.         bottom = i == row-1
  105.         args.each_with_index do |arr, n|
  106.           begin_ = n == 0
  107.           end_ = n == args.length - 1
  108.           col = COLS[n]
  109.           diff = args[0].length - arr.length
  110.           diff = 0 if diff < 0
  111.           str = (i < diff ? '' : arr[i-diff]) || ''
  112.           str = str.send(col[2] == :L ? :ljust : :rjust, col[0])
  113.           if SPLIT_LINE and !end_
  114.             hmar = (MARGIN - 1) / 2
  115.             str << ' ' * hmar << '|' << ' ' * (MARGIN - hmar - 1)
  116.           else
  117.             str << ' ' * MARGIN
  118.           end
  119.           str << "\n" if end_
  120.           print str
  121.         end
  122.       end
  123.       @row += 1
  124.     end
  125.   end
  126. end

鸡蛋

鲜花

评论 (0 个评论)

facelist doodle 涂鸦笔

您需要登录后才可以评论 登录 | 注册会员

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-4-28 01:04

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

返回顶部