Project1
标题:
【Benchmark】代码执行效率测试脚本
[打印本页]
作者:
张咚咚
时间:
2023-1-15 11:22
标题:
【Benchmark】代码执行效率测试脚本
脚本如下,设置可随意更改,打印完美输出。
图片预览
#encoding:utf-8
#==============================================================================
# ■ Benchmark v1.0 By QQEat
#------------------------------------------------------------------------------
# 基准测试(优雅 + 完美强迫症打印)
#==============================================================================
#
# # 打印代码块执行时间(count 默认 1, 可在下面设置更改)
# Benchmark.measure(count){|r| ... }
#
# # 测试代码快效率
# Benchmark.measure(10_000) { [*1..1000].shuffle }
#
# # 汇报打印
# Benchmark.measure do |x|
# a = (1..100).to_a.shuffle!
# x.report('Array#sort', 100000) { a.sort }
# x.report('Array#sort!', 100000) { a.sort! }
# 10.times{ x.report{ a.sort! } }
# end
#
module Benchmark
#--------------------------------------------------------------------------
# ● 常量
#--------------------------------------------------------------------------
ITERATIONS = 1 # 默认执行次数
MARGIN = 3 # 列间距
SPLIT_LINE = true # 是否显示列分割线
COLS = [
[7, '#', :R],
[10, 'user', :R], # 用户时间
[10, 'system', :R], # 系统时间
[10, 'real', :R], # 真实用时
[10, 'avg', :R], # 平均用时
[10, 'times', :R], # 执行次数
].map.with_index{|v, i|
[v[0], v[1].send(v[2] == :L ? :ljust : :rjust, v[0]) + ' ' * MARGIN, v[2]]
} # [列宽, 列名, 对齐方向]
#==============================================================================
# ■ Reporter
#==============================================================================
class Reporter
#--------------------------------------------------------------------------
# ● 初始化
#--------------------------------------------------------------------------
def initialize
@total = [0, 0, 0, 0]
end
#--------------------------------------------------------------------------
# ● 汇报执行(标签名, 执行次数)
#--------------------------------------------------------------------------
def report(label = nil, iterate = ITERATIONS)
result = Benchmark.send(:time, iterate) { yield }
result << result[-1] / iterate
Benchmark.send(:output, label, *result.map{|v| '%0.6f' % v }, iterate)
@total.map!.with_index { |t, i| t += result[i].to_f }
end
end
#--------------------------------------------------------------------------
# ● measure 测量脚本执行时间
#--------------------------------------------------------------------------
def self.measure(iterate = ITERATIONS, &block)
@row = 0
width = COLS.inject(0){|a,b| a+b[1].length } / 2 - 5
puts "#{'-'*width} BENCHMARK #{'-'*width}"
puts COLS.map{|v| v[1] }.join
reporter = Reporter.new
if block.arity == 0
result = time(iterate) { yield }
else
yield reporter
result = reporter.instance_variable_get(:@total)
end
print "\n"
output('Total', *result.map{|v| '%0.6f' % v }, '')
print "\n"
end
class << self
private
#--------------------------------------------------------------------------
# ● 执行脚本(执行次数)
# return:[用户时间, 系统时间, 真实时间]
#--------------------------------------------------------------------------
def time(iterate = ITERATIONS)
t = Process.times
initial = [t.utime, t.stime, Time.now]
iterate.times { yield }
t2 = Process.times
[t2.utime, t2.stime, Time.now].map!.with_index do |time, index|
time - initial[index]
end
end
#--------------------------------------------------------------------------
# ● 打印输出
#--------------------------------------------------------------------------
def output(*args)
args[-1] = args[-1].to_s.gsub(/(?=(?!\b)(\d{3})+$)/, ',')
(args[0] ||= @row.to_s) << ':'
args.map!.with_index{|v,i| v.to_s.scan(/.{1,#{COLS[i][0]}}/) }
row = args.max_by{|v| v.length }.length
row.times do |i|
bottom = i == row-1
args.each_with_index do |arr, n|
begin_ = n == 0
end_ = n == args.length - 1
col = COLS[n]
diff = args[0].length - arr.length
diff = 0 if diff < 0
str = (i < diff ? '' : arr[i-diff]) || ''
str = str.send(col[2] == :L ? :ljust : :rjust, col[0])
if SPLIT_LINE and !end_
hmar = (MARGIN - 1) / 2
str << ' ' * hmar << '|' << ' ' * (MARGIN - hmar - 1)
else
str << ' ' * MARGIN
end
str << "\n" if end_
print str
end
end
@row += 1
end
end
end
复制代码
作者:
百里_飞柳
时间:
2023-1-15 20:32
2302年了,居然还能看到rgss3的基础组件
欢迎光临 Project1 (https://rpg.blue/)
Powered by Discuz! X3.1