Permalink
Browse files

more details in the readme, cleanup/performance optimization

  • Loading branch information...
1 parent 0c1f748 commit 3b50104b87c60af766d876f1dc78ddf3844a7b23 geemus (Wesley Beary) committed Apr 8, 2010
Showing with 23 additions and 12 deletions.
  1. +4 −0 README.rdoc
  2. +19 −12 lib/tach.rb
View
@@ -26,6 +26,10 @@ Then inside the tach block you should specify the code you care about comparing.
The output will show you the progress through each tach and average/total real times for each one when they finish.
Each tach will be run all its repetitions in its own thread, with a new thread for each tach.
+BUT BUT, why does my progressbar take longer than the total time listed in the table?
+Well, displaying the progressbar is kinda slow, but that time doesn't count against the total for the tach.
+I'll be using tach to help me make it go faster, and then I'll (hopefully remember to) remove this notice!
+
== Copyright
(The MIT License)
View
@@ -19,15 +19,15 @@ def initialize(times = 1, &block)
Formatador.display_line
longest = @benchmarks.map {|benchmark| benchmark[0]}.map {|name| name.length}.max
- @benchmarks.each do |name, block|
- @results[name] = run_in_thread("#{name}#{' ' * (longest - name.length)}", @times, block)
+ for name, block in @benchmarks
+ @results[name] = run_in_thread("#{name}#{' ' * (longest - name.length)}", @times, &block)
end
data = []
- @benchmarks.each do |name, block|
+ for name, block in @benchmarks
value = @results[name]
total = value.inject(0) {|sum,item| sum + item}
- data << { :average => format("%.5f", (total / value.length)), :tach => name, :total => format("%.5f", total) }
+ data << { :average => format("%8.6f", (total / value.length)), :tach => name, :total => format("%8.6f", total) }
end
Formatador.display_table(data, [:tach, :average, :total])
@@ -40,17 +40,24 @@ def tach(name, &block)
private
- def run_in_thread(name, count, benchmark)
+ def run_in_thread(name, count, &benchmark)
+ if count.to_s.length > 3
+ divisor = 1
+ (count.to_s.length - 3).times do
+ divisor *= 10
+ end
+ end
thread = Thread.new {
- thread_start = Time.now
- Formatador.redisplay_progressbar(0, count, :label => name, :started_at => thread_start)
- count.times do |index|
+ Thread.current[:results] ||= []
+ Thread.current[:started_at] = Time.now
+ Formatador.redisplay_progressbar(0, count, :label => name, :started_at => Thread.current[:started_at])
+ for index in 1..count
tach_start = Time.now
instance_eval(&benchmark)
- tach_elapsed = Time.now.to_f - tach_start.to_f
- Thread.current[:results] ||= []
- Thread.current[:results] << tach_elapsed
- Formatador.redisplay_progressbar(index + 1, count, :label => name, :started_at => thread_start)
+ Thread.current[:results] << Time.now.to_f - tach_start.to_f
+ if !divisor || index % divisor == 0
+ Formatador.redisplay_progressbar(index, count, :label => name, :started_at => Thread.current[:started_at])
+ end
end
Formatador.display_line
}

0 comments on commit 3b50104

Please sign in to comment.