File: cps.rb

package info (click to toggle)
librexml-ruby 1.2.5-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 792 kB
  • ctags: 655
  • sloc: ruby: 3,778; xml: 1,609; java: 109; makefile: 43
file content (26 lines) | stat: -rw-r--r-- 425 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class CPS
	def initialize secs=20
		@seconds = secs
	end

	def time(name=nil, &block)
		printf "%-45s", "#{name} for #@seconds seconds"
		$stdout.flush
		@count = 0
		thread = Thread.new {
			while true
				block.call
				@count += 1
			end
		}

		sleep @seconds
		thread.kill
		printf "%-20s", " ... #@count calls"
		cps = @count.to_f/@seconds.to_f
		num = "%0.2f"%cps
		printf "%-14s", " (#{num}/s)"
		puts 
		cps
	end
end