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
