mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	 8fded5ba3e
			
		
	
	
		8fded5ba3e
		
	
	
	
	
		
			
			* sample/benchmark.rb: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46922 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
		
			
				
	
	
		
			19 lines
		
	
	
	
		
			507 B
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			19 lines
		
	
	
	
		
			507 B
		
	
	
	
		
			Ruby
		
	
	
	
	
	
| require 'benchmark'
 | |
| 
 | |
| include Benchmark
 | |
| 
 | |
| n = ARGV[0].to_i.nonzero? || 50000
 | |
| puts %Q([#{n} times iterations of `a = "1"'])
 | |
| benchmark(CAPTION, 7, FORMAT) do |x|
 | |
|   x.report("for:")   {for _ in 1..n; _ = "1"; end} # Benchmark.measure
 | |
|   x.report("times:") {n.times do   ; _ = "1"; end}
 | |
|   x.report("upto:")  {1.upto(n) do ; _ = "1"; end}
 | |
| end
 | |
| 
 | |
| benchmark do
 | |
|   [
 | |
|     measure{for _ in 1..n; _ = "1"; end},  # Benchmark.measure
 | |
|     measure{n.times do   ; _ = "1"; end},
 | |
|     measure{1.upto(n) do ; _ = "1"; end}
 | |
|   ]
 | |
| end
 |