зеркало из https://github.com/github/ruby.git
* set eol-style.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13944 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
Родитель
501407d3af
Коммит
75feee0968
|
@ -1,26 +1,26 @@
|
|||
#
|
||||
# Create many HTML strings with ERB.
|
||||
#
|
||||
|
||||
require 'erb'
|
||||
|
||||
data = DATA.read
|
||||
max = 5_000
|
||||
title = "hello world!"
|
||||
content = "hello world!\n" * 10
|
||||
|
||||
max.times{
|
||||
ERB.new(data).result(binding)
|
||||
}
|
||||
|
||||
__END__
|
||||
|
||||
<html>
|
||||
<head> <%= title %> </head>
|
||||
<body>
|
||||
<h1> <%= title %> </h1>
|
||||
<p>
|
||||
<%= content %>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
#
|
||||
# Create many HTML strings with ERB.
|
||||
#
|
||||
|
||||
require 'erb'
|
||||
|
||||
data = DATA.read
|
||||
max = 5_000
|
||||
title = "hello world!"
|
||||
content = "hello world!\n" * 10
|
||||
|
||||
max.times{
|
||||
ERB.new(data).result(binding)
|
||||
}
|
||||
|
||||
__END__
|
||||
|
||||
<html>
|
||||
<head> <%= title %> </head>
|
||||
<body>
|
||||
<h1> <%= title %> </h1>
|
||||
<p>
|
||||
<%= content %>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
require 'uri'
|
||||
|
||||
100_000.times{
|
||||
uri = URI.parse('http://www.ruby-lang.org')
|
||||
uri.scheme
|
||||
uri.host
|
||||
uri.port
|
||||
}
|
||||
require 'uri'
|
||||
|
||||
100_000.times{
|
||||
uri = URI.parse('http://www.ruby-lang.org')
|
||||
uri.scheme
|
||||
uri.host
|
||||
uri.port
|
||||
}
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
#
|
||||
# Create files
|
||||
#
|
||||
|
||||
max = 50_000
|
||||
file = './tmpfile_of_bm_io_file_create'
|
||||
|
||||
max.times{
|
||||
f = open(file, 'w')
|
||||
f.close#(true)
|
||||
}
|
||||
File.unlink(file)
|
||||
|
||||
#
|
||||
# Create files
|
||||
#
|
||||
|
||||
max = 50_000
|
||||
file = './tmpfile_of_bm_io_file_create'
|
||||
|
||||
max.times{
|
||||
f = open(file, 'w')
|
||||
f.close#(true)
|
||||
}
|
||||
File.unlink(file)
|
||||
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
#
|
||||
# Seek and Read file.
|
||||
#
|
||||
|
||||
require 'tempfile'
|
||||
|
||||
max = 20_000
|
||||
str = "Hello world! " * 1000
|
||||
f = Tempfile.new('yarv-benchmark')
|
||||
f.write str
|
||||
|
||||
max.times{
|
||||
f.seek 0
|
||||
f.read
|
||||
}
|
||||
#
|
||||
# Seek and Read file.
|
||||
#
|
||||
|
||||
require 'tempfile'
|
||||
|
||||
max = 20_000
|
||||
str = "Hello world! " * 1000
|
||||
f = Tempfile.new('yarv-benchmark')
|
||||
f.write str
|
||||
|
||||
max.times{
|
||||
f.seek 0
|
||||
f.read
|
||||
}
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
#
|
||||
# Seek and Write file.
|
||||
#
|
||||
|
||||
require 'tempfile'
|
||||
|
||||
max = 20_000
|
||||
str = "Hello world! " * 1000
|
||||
f = Tempfile.new('yarv-benchmark')
|
||||
|
||||
max.times{
|
||||
f.seek 0
|
||||
f.write str
|
||||
}
|
||||
#
|
||||
# Seek and Write file.
|
||||
#
|
||||
|
||||
require 'tempfile'
|
||||
|
||||
max = 20_000
|
||||
str = "Hello world! " * 1000
|
||||
f = Tempfile.new('yarv-benchmark')
|
||||
|
||||
max.times{
|
||||
f.seek 0
|
||||
f.write str
|
||||
}
|
||||
|
|
|
@ -1,57 +1,57 @@
|
|||
# The Computer Language Shootout Benchmarks
|
||||
# http://shootout.alioth.debian.org
|
||||
#
|
||||
# contributed by Jesse Millikan
|
||||
|
||||
# disable output
|
||||
def STDOUT.write_ *args
|
||||
end
|
||||
|
||||
def item_check(tree)
|
||||
if tree[0] == nil
|
||||
tree[1]
|
||||
else
|
||||
tree[1] + item_check(tree[0]) - item_check(tree[2])
|
||||
end
|
||||
end
|
||||
|
||||
def bottom_up_tree(item, depth)
|
||||
if depth > 0
|
||||
item_item = 2 * item
|
||||
depth -= 1
|
||||
[bottom_up_tree(item_item - 1, depth), item, bottom_up_tree(item_item, depth)]
|
||||
else
|
||||
[nil, item, nil]
|
||||
end
|
||||
end
|
||||
|
||||
max_depth = 12 # 16 # ARGV[0].to_i
|
||||
min_depth = 4
|
||||
|
||||
max_depth = min_depth + 2 if min_depth + 2 > max_depth
|
||||
|
||||
stretch_depth = max_depth + 1
|
||||
stretch_tree = bottom_up_tree(0, stretch_depth)
|
||||
|
||||
puts "stretch tree of depth #{stretch_depth}\t check: #{item_check(stretch_tree)}"
|
||||
stretch_tree = nil
|
||||
|
||||
long_lived_tree = bottom_up_tree(0, max_depth)
|
||||
|
||||
min_depth.step(max_depth + 1, 2) do |depth|
|
||||
iterations = 2**(max_depth - depth + min_depth)
|
||||
|
||||
check = 0
|
||||
|
||||
for i in 1..iterations
|
||||
temp_tree = bottom_up_tree(i, depth)
|
||||
check += item_check(temp_tree)
|
||||
|
||||
temp_tree = bottom_up_tree(-i, depth)
|
||||
check += item_check(temp_tree)
|
||||
end
|
||||
|
||||
puts "#{iterations * 2}\t trees of depth #{depth}\t check: #{check}"
|
||||
end
|
||||
|
||||
puts "long lived tree of depth #{max_depth}\t check: #{item_check(long_lived_tree)}"
|
||||
# The Computer Language Shootout Benchmarks
|
||||
# http://shootout.alioth.debian.org
|
||||
#
|
||||
# contributed by Jesse Millikan
|
||||
|
||||
# disable output
|
||||
def STDOUT.write_ *args
|
||||
end
|
||||
|
||||
def item_check(tree)
|
||||
if tree[0] == nil
|
||||
tree[1]
|
||||
else
|
||||
tree[1] + item_check(tree[0]) - item_check(tree[2])
|
||||
end
|
||||
end
|
||||
|
||||
def bottom_up_tree(item, depth)
|
||||
if depth > 0
|
||||
item_item = 2 * item
|
||||
depth -= 1
|
||||
[bottom_up_tree(item_item - 1, depth), item, bottom_up_tree(item_item, depth)]
|
||||
else
|
||||
[nil, item, nil]
|
||||
end
|
||||
end
|
||||
|
||||
max_depth = 12 # 16 # ARGV[0].to_i
|
||||
min_depth = 4
|
||||
|
||||
max_depth = min_depth + 2 if min_depth + 2 > max_depth
|
||||
|
||||
stretch_depth = max_depth + 1
|
||||
stretch_tree = bottom_up_tree(0, stretch_depth)
|
||||
|
||||
puts "stretch tree of depth #{stretch_depth}\t check: #{item_check(stretch_tree)}"
|
||||
stretch_tree = nil
|
||||
|
||||
long_lived_tree = bottom_up_tree(0, max_depth)
|
||||
|
||||
min_depth.step(max_depth + 1, 2) do |depth|
|
||||
iterations = 2**(max_depth - depth + min_depth)
|
||||
|
||||
check = 0
|
||||
|
||||
for i in 1..iterations
|
||||
temp_tree = bottom_up_tree(i, depth)
|
||||
check += item_check(temp_tree)
|
||||
|
||||
temp_tree = bottom_up_tree(-i, depth)
|
||||
check += item_check(temp_tree)
|
||||
end
|
||||
|
||||
puts "#{iterations * 2}\t trees of depth #{depth}\t check: #{check}"
|
||||
end
|
||||
|
||||
puts "long lived tree of depth #{max_depth}\t check: #{item_check(long_lived_tree)}"
|
||||
|
|
|
@ -1,45 +1,45 @@
|
|||
# The Computer Language Shootout
|
||||
# http://shootout.alioth.debian.org/
|
||||
# Contributed by Sokolov Yura
|
||||
# Modified by Ryan Williams
|
||||
|
||||
def fannkuch(n)
|
||||
maxFlips, m, r, check = 0, n-1, n, 0
|
||||
count = (1..n).to_a
|
||||
perm = (1..n).to_a
|
||||
|
||||
while true
|
||||
if check < 30
|
||||
puts "#{perm}"
|
||||
check += 1
|
||||
end
|
||||
|
||||
while r != 1
|
||||
count[r-1] = r
|
||||
r -= 1
|
||||
end
|
||||
|
||||
if perm[0] != 1 and perm[m] != n
|
||||
perml = perm.clone #.dup
|
||||
flips = 0
|
||||
while (k = perml.first ) != 1
|
||||
perml = perml.slice!(0, k).reverse + perml
|
||||
flips += 1
|
||||
end
|
||||
maxFlips = flips if flips > maxFlips
|
||||
end
|
||||
while true
|
||||
if r==n then return maxFlips end
|
||||
perm.insert r,perm.shift
|
||||
break if (count[r] -= 1) > 0
|
||||
r += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def puts *args
|
||||
end
|
||||
|
||||
N = 10 # (ARGV[0] || 1).to_i
|
||||
puts "Pfannkuchen(#{N}) = #{fannkuch(N)}"
|
||||
|
||||
# The Computer Language Shootout
|
||||
# http://shootout.alioth.debian.org/
|
||||
# Contributed by Sokolov Yura
|
||||
# Modified by Ryan Williams
|
||||
|
||||
def fannkuch(n)
|
||||
maxFlips, m, r, check = 0, n-1, n, 0
|
||||
count = (1..n).to_a
|
||||
perm = (1..n).to_a
|
||||
|
||||
while true
|
||||
if check < 30
|
||||
puts "#{perm}"
|
||||
check += 1
|
||||
end
|
||||
|
||||
while r != 1
|
||||
count[r-1] = r
|
||||
r -= 1
|
||||
end
|
||||
|
||||
if perm[0] != 1 and perm[m] != n
|
||||
perml = perm.clone #.dup
|
||||
flips = 0
|
||||
while (k = perml.first ) != 1
|
||||
perml = perml.slice!(0, k).reverse + perml
|
||||
flips += 1
|
||||
end
|
||||
maxFlips = flips if flips > maxFlips
|
||||
end
|
||||
while true
|
||||
if r==n then return maxFlips end
|
||||
perm.insert r,perm.shift
|
||||
break if (count[r] -= 1) > 0
|
||||
r += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def puts *args
|
||||
end
|
||||
|
||||
N = 10 # (ARGV[0] || 1).to_i
|
||||
puts "Pfannkuchen(#{N}) = #{fannkuch(N)}"
|
||||
|
||||
|
|
|
@ -1,81 +1,81 @@
|
|||
# The Computer Language Shootout
|
||||
# http://shootout.alioth.debian.org/
|
||||
# Contributed by Sokolov Yura
|
||||
|
||||
$last = 42.0
|
||||
def gen_random (max,im=139968,ia=3877,ic=29573)
|
||||
(max * ($last = ($last * ia + ic) % im)) / im
|
||||
end
|
||||
|
||||
alu =
|
||||
"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG"+
|
||||
"GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA"+
|
||||
"CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT"+
|
||||
"ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA"+
|
||||
"GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG"+
|
||||
"AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC"+
|
||||
"AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA"
|
||||
|
||||
iub = [
|
||||
["a", 0.27],
|
||||
["c", 0.12],
|
||||
["g", 0.12],
|
||||
["t", 0.27],
|
||||
|
||||
["B", 0.02],
|
||||
["D", 0.02],
|
||||
["H", 0.02],
|
||||
["K", 0.02],
|
||||
["M", 0.02],
|
||||
["N", 0.02],
|
||||
["R", 0.02],
|
||||
["S", 0.02],
|
||||
["V", 0.02],
|
||||
["W", 0.02],
|
||||
["Y", 0.02],
|
||||
]
|
||||
homosapiens = [
|
||||
["a", 0.3029549426680],
|
||||
["c", 0.1979883004921],
|
||||
["g", 0.1975473066391],
|
||||
["t", 0.3015094502008],
|
||||
]
|
||||
|
||||
def make_repeat_fasta(id, desc, src, n)
|
||||
puts ">#{id} #{desc}"
|
||||
v = nil
|
||||
width = 60
|
||||
l = src.length
|
||||
s = src * ((n / l) + 1)
|
||||
s.slice!(n, l)
|
||||
puts(s.scan(/.{1,#{width}}/).join("\n"))
|
||||
end
|
||||
|
||||
def make_random_fasta(id, desc, table, n)
|
||||
puts ">#{id} #{desc}"
|
||||
rand, v = nil,nil
|
||||
width = 60
|
||||
chunk = 1 * width
|
||||
prob = 0.0
|
||||
table.each{|v| v[1]= (prob += v[1])}
|
||||
for i in 1..(n/width)
|
||||
puts((1..width).collect{
|
||||
rand = gen_random(1.0)
|
||||
table.find{|v| v[1]>rand}[0]
|
||||
}.join)
|
||||
end
|
||||
if n%width != 0
|
||||
puts((1..(n%width)).collect{
|
||||
rand = gen_random(1.0)
|
||||
table.find{|v| v[1]>rand}[0]
|
||||
}.join)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
n = (ARGV[0] or 250_000).to_i
|
||||
|
||||
make_repeat_fasta('ONE', 'Homo sapiens alu', alu, n*2)
|
||||
make_random_fasta('TWO', 'IUB ambiguity codes', iub, n*3)
|
||||
make_random_fasta('THREE', 'Homo sapiens frequency', homosapiens, n*5)
|
||||
|
||||
# The Computer Language Shootout
|
||||
# http://shootout.alioth.debian.org/
|
||||
# Contributed by Sokolov Yura
|
||||
|
||||
$last = 42.0
|
||||
def gen_random (max,im=139968,ia=3877,ic=29573)
|
||||
(max * ($last = ($last * ia + ic) % im)) / im
|
||||
end
|
||||
|
||||
alu =
|
||||
"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG"+
|
||||
"GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA"+
|
||||
"CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT"+
|
||||
"ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA"+
|
||||
"GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG"+
|
||||
"AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC"+
|
||||
"AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA"
|
||||
|
||||
iub = [
|
||||
["a", 0.27],
|
||||
["c", 0.12],
|
||||
["g", 0.12],
|
||||
["t", 0.27],
|
||||
|
||||
["B", 0.02],
|
||||
["D", 0.02],
|
||||
["H", 0.02],
|
||||
["K", 0.02],
|
||||
["M", 0.02],
|
||||
["N", 0.02],
|
||||
["R", 0.02],
|
||||
["S", 0.02],
|
||||
["V", 0.02],
|
||||
["W", 0.02],
|
||||
["Y", 0.02],
|
||||
]
|
||||
homosapiens = [
|
||||
["a", 0.3029549426680],
|
||||
["c", 0.1979883004921],
|
||||
["g", 0.1975473066391],
|
||||
["t", 0.3015094502008],
|
||||
]
|
||||
|
||||
def make_repeat_fasta(id, desc, src, n)
|
||||
puts ">#{id} #{desc}"
|
||||
v = nil
|
||||
width = 60
|
||||
l = src.length
|
||||
s = src * ((n / l) + 1)
|
||||
s.slice!(n, l)
|
||||
puts(s.scan(/.{1,#{width}}/).join("\n"))
|
||||
end
|
||||
|
||||
def make_random_fasta(id, desc, table, n)
|
||||
puts ">#{id} #{desc}"
|
||||
rand, v = nil,nil
|
||||
width = 60
|
||||
chunk = 1 * width
|
||||
prob = 0.0
|
||||
table.each{|v| v[1]= (prob += v[1])}
|
||||
for i in 1..(n/width)
|
||||
puts((1..width).collect{
|
||||
rand = gen_random(1.0)
|
||||
table.find{|v| v[1]>rand}[0]
|
||||
}.join)
|
||||
end
|
||||
if n%width != 0
|
||||
puts((1..(n%width)).collect{
|
||||
rand = gen_random(1.0)
|
||||
table.find{|v| v[1]>rand}[0]
|
||||
}.join)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
n = (ARGV[0] or 250_000).to_i
|
||||
|
||||
make_repeat_fasta('ONE', 'Homo sapiens alu', alu, n*2)
|
||||
make_random_fasta('TWO', 'IUB ambiguity codes', iub, n*3)
|
||||
make_random_fasta('THREE', 'Homo sapiens frequency', homosapiens, n*5)
|
||||
|
||||
|
|
|
@ -1,48 +1,48 @@
|
|||
# The Computer Language Shootout
|
||||
# http://shootout.alioth.debian.org
|
||||
#
|
||||
# contributed by jose fco. gonzalez
|
||||
# modified by Sokolov Yura
|
||||
|
||||
seq = String.new
|
||||
|
||||
def frecuency( seq,length )
|
||||
n, table = seq.length - length + 1, Hash.new(0)
|
||||
f, i = nil, nil
|
||||
(0 ... length).each do |f|
|
||||
(f ... n).step(length) do |i|
|
||||
table[seq[i,length]] += 1
|
||||
end
|
||||
end
|
||||
[n,table]
|
||||
|
||||
end
|
||||
|
||||
def sort_by_freq( seq,length )
|
||||
n,table = frecuency( seq,length )
|
||||
a, b, v = nil, nil, nil
|
||||
table.sort{|a,b| b[1] <=> a[1]}.each do |v|
|
||||
puts "%s %.3f" % [v[0].upcase,((v[1]*100).to_f/n)]
|
||||
end
|
||||
puts
|
||||
end
|
||||
|
||||
def find_seq( seq,s )
|
||||
n,table = frecuency( seq,s.length )
|
||||
puts "#{table[s].to_s}\t#{s.upcase}"
|
||||
end
|
||||
|
||||
input = open(File.join(File.dirname($0), 'fasta.output.100000'), 'rb')
|
||||
|
||||
line = input.gets while line !~ /^>THREE/
|
||||
line = input.gets
|
||||
|
||||
while (line !~ /^>/) & line do
|
||||
seq << line.chomp
|
||||
line = input.gets
|
||||
end
|
||||
|
||||
[1,2].each {|i| sort_by_freq( seq,i ) }
|
||||
|
||||
%w(ggt ggta ggtatt ggtattttaatt ggtattttaatttatagt).each{|s| find_seq( seq,s) }
|
||||
|
||||
# The Computer Language Shootout
|
||||
# http://shootout.alioth.debian.org
|
||||
#
|
||||
# contributed by jose fco. gonzalez
|
||||
# modified by Sokolov Yura
|
||||
|
||||
seq = String.new
|
||||
|
||||
def frecuency( seq,length )
|
||||
n, table = seq.length - length + 1, Hash.new(0)
|
||||
f, i = nil, nil
|
||||
(0 ... length).each do |f|
|
||||
(f ... n).step(length) do |i|
|
||||
table[seq[i,length]] += 1
|
||||
end
|
||||
end
|
||||
[n,table]
|
||||
|
||||
end
|
||||
|
||||
def sort_by_freq( seq,length )
|
||||
n,table = frecuency( seq,length )
|
||||
a, b, v = nil, nil, nil
|
||||
table.sort{|a,b| b[1] <=> a[1]}.each do |v|
|
||||
puts "%s %.3f" % [v[0].upcase,((v[1]*100).to_f/n)]
|
||||
end
|
||||
puts
|
||||
end
|
||||
|
||||
def find_seq( seq,s )
|
||||
n,table = frecuency( seq,s.length )
|
||||
puts "#{table[s].to_s}\t#{s.upcase}"
|
||||
end
|
||||
|
||||
input = open(File.join(File.dirname($0), 'fasta.output.100000'), 'rb')
|
||||
|
||||
line = input.gets while line !~ /^>THREE/
|
||||
line = input.gets
|
||||
|
||||
while (line !~ /^>/) & line do
|
||||
seq << line.chomp
|
||||
line = input.gets
|
||||
end
|
||||
|
||||
[1,2].each {|i| sort_by_freq( seq,i ) }
|
||||
|
||||
%w(ggt ggta ggtatt ggtattttaatt ggtattttaatttatagt).each{|s| find_seq( seq,s) }
|
||||
|
||||
|
|
|
@ -1,57 +1,57 @@
|
|||
# The Computer Language Benchmarks Game
|
||||
# http://shootout.alioth.debian.org/
|
||||
#
|
||||
# contributed by Karl von Laudermann
|
||||
# modified by Jeremy Echols
|
||||
|
||||
size = 600 # ARGV[0].to_i
|
||||
|
||||
puts "P4\n#{size} #{size}"
|
||||
|
||||
ITER = 49 # Iterations - 1 for easy for..in looping
|
||||
LIMIT_SQUARED = 4.0 # Presquared limit
|
||||
|
||||
byte_acc = 0
|
||||
bit_num = 0
|
||||
|
||||
count_size = size - 1 # Precomputed size for easy for..in looping
|
||||
|
||||
# For..in loops are faster than .upto, .downto, .times, etc.
|
||||
for y in 0..count_size
|
||||
for x in 0..count_size
|
||||
zr = 0.0
|
||||
zi = 0.0
|
||||
cr = (2.0*x/size)-1.5
|
||||
ci = (2.0*y/size)-1.0
|
||||
escape = false
|
||||
|
||||
# To make use of the for..in code, we use a dummy variable,
|
||||
# like one would in C
|
||||
for dummy in 0..ITER
|
||||
tr = zr*zr - zi*zi + cr
|
||||
ti = 2*zr*zi + ci
|
||||
zr, zi = tr, ti
|
||||
|
||||
if (zr*zr+zi*zi) > LIMIT_SQUARED
|
||||
escape = true
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
byte_acc = (byte_acc << 1) | (escape ? 0b0 : 0b1)
|
||||
bit_num += 1
|
||||
|
||||
# Code is very similar for these cases, but using separate blocks
|
||||
# ensures we skip the shifting when it's unnecessary, which is most cases.
|
||||
if (bit_num == 8)
|
||||
print byte_acc.chr
|
||||
byte_acc = 0
|
||||
bit_num = 0
|
||||
elsif (x == count_size)
|
||||
byte_acc <<= (8 - bit_num)
|
||||
print byte_acc.chr
|
||||
byte_acc = 0
|
||||
bit_num = 0
|
||||
end
|
||||
end
|
||||
end
|
||||
# The Computer Language Benchmarks Game
|
||||
# http://shootout.alioth.debian.org/
|
||||
#
|
||||
# contributed by Karl von Laudermann
|
||||
# modified by Jeremy Echols
|
||||
|
||||
size = 600 # ARGV[0].to_i
|
||||
|
||||
puts "P4\n#{size} #{size}"
|
||||
|
||||
ITER = 49 # Iterations - 1 for easy for..in looping
|
||||
LIMIT_SQUARED = 4.0 # Presquared limit
|
||||
|
||||
byte_acc = 0
|
||||
bit_num = 0
|
||||
|
||||
count_size = size - 1 # Precomputed size for easy for..in looping
|
||||
|
||||
# For..in loops are faster than .upto, .downto, .times, etc.
|
||||
for y in 0..count_size
|
||||
for x in 0..count_size
|
||||
zr = 0.0
|
||||
zi = 0.0
|
||||
cr = (2.0*x/size)-1.5
|
||||
ci = (2.0*y/size)-1.0
|
||||
escape = false
|
||||
|
||||
# To make use of the for..in code, we use a dummy variable,
|
||||
# like one would in C
|
||||
for dummy in 0..ITER
|
||||
tr = zr*zr - zi*zi + cr
|
||||
ti = 2*zr*zi + ci
|
||||
zr, zi = tr, ti
|
||||
|
||||
if (zr*zr+zi*zi) > LIMIT_SQUARED
|
||||
escape = true
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
byte_acc = (byte_acc << 1) | (escape ? 0b0 : 0b1)
|
||||
bit_num += 1
|
||||
|
||||
# Code is very similar for these cases, but using separate blocks
|
||||
# ensures we skip the shifting when it's unnecessary, which is most cases.
|
||||
if (bit_num == 8)
|
||||
print byte_acc.chr
|
||||
byte_acc = 0
|
||||
bit_num = 0
|
||||
elsif (x == count_size)
|
||||
byte_acc <<= (8 - bit_num)
|
||||
print byte_acc.chr
|
||||
byte_acc = 0
|
||||
bit_num = 0
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -1,148 +1,148 @@
|
|||
# The Computer Language Shootout
|
||||
# http://shootout.alioth.debian.org
|
||||
#
|
||||
# Optimized for Ruby by Jesse Millikan
|
||||
# From version ported by Michael Neumann from the C gcc version,
|
||||
# which was written by Christoph Bauer.
|
||||
|
||||
SOLAR_MASS = 4 * Math::PI**2
|
||||
DAYS_PER_YEAR = 365.24
|
||||
|
||||
def _puts *args
|
||||
end
|
||||
|
||||
class Planet
|
||||
attr_accessor :x, :y, :z, :vx, :vy, :vz, :mass
|
||||
|
||||
def initialize(x, y, z, vx, vy, vz, mass)
|
||||
@x, @y, @z = x, y, z
|
||||
@vx, @vy, @vz = vx * DAYS_PER_YEAR, vy * DAYS_PER_YEAR, vz * DAYS_PER_YEAR
|
||||
@mass = mass * SOLAR_MASS
|
||||
end
|
||||
|
||||
def move_from_i(bodies, nbodies, dt, i)
|
||||
while i < nbodies
|
||||
b2 = bodies[i]
|
||||
dx = @x - b2.x
|
||||
dy = @y - b2.y
|
||||
dz = @z - b2.z
|
||||
|
||||
distance = Math.sqrt(dx * dx + dy * dy + dz * dz)
|
||||
mag = dt / (distance * distance * distance)
|
||||
b_mass_mag, b2_mass_mag = @mass * mag, b2.mass * mag
|
||||
|
||||
@vx -= dx * b2_mass_mag
|
||||
@vy -= dy * b2_mass_mag
|
||||
@vz -= dz * b2_mass_mag
|
||||
b2.vx += dx * b_mass_mag
|
||||
b2.vy += dy * b_mass_mag
|
||||
b2.vz += dz * b_mass_mag
|
||||
i += 1
|
||||
end
|
||||
|
||||
@x += dt * @vx
|
||||
@y += dt * @vy
|
||||
@z += dt * @vz
|
||||
end
|
||||
end
|
||||
|
||||
def energy(bodies)
|
||||
e = 0.0
|
||||
nbodies = bodies.size
|
||||
|
||||
for i in 0 ... nbodies
|
||||
b = bodies[i]
|
||||
e += 0.5 * b.mass * (b.vx * b.vx + b.vy * b.vy + b.vz * b.vz)
|
||||
for j in (i + 1) ... nbodies
|
||||
b2 = bodies[j]
|
||||
dx = b.x - b2.x
|
||||
dy = b.y - b2.y
|
||||
dz = b.z - b2.z
|
||||
distance = Math.sqrt(dx * dx + dy * dy + dz * dz)
|
||||
e -= (b.mass * b2.mass) / distance
|
||||
end
|
||||
end
|
||||
e
|
||||
end
|
||||
|
||||
def offset_momentum(bodies)
|
||||
px, py, pz = 0.0, 0.0, 0.0
|
||||
|
||||
for b in bodies
|
||||
m = b.mass
|
||||
px += b.vx * m
|
||||
py += b.vy * m
|
||||
pz += b.vz * m
|
||||
end
|
||||
|
||||
b = bodies[0]
|
||||
b.vx = - px / SOLAR_MASS
|
||||
b.vy = - py / SOLAR_MASS
|
||||
b.vz = - pz / SOLAR_MASS
|
||||
end
|
||||
|
||||
BODIES = [
|
||||
# sun
|
||||
Planet.new(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0),
|
||||
|
||||
# jupiter
|
||||
Planet.new(
|
||||
4.84143144246472090e+00,
|
||||
-1.16032004402742839e+00,
|
||||
-1.03622044471123109e-01,
|
||||
1.66007664274403694e-03,
|
||||
7.69901118419740425e-03,
|
||||
-6.90460016972063023e-05,
|
||||
9.54791938424326609e-04),
|
||||
|
||||
# saturn
|
||||
Planet.new(
|
||||
8.34336671824457987e+00,
|
||||
4.12479856412430479e+00,
|
||||
-4.03523417114321381e-01,
|
||||
-2.76742510726862411e-03,
|
||||
4.99852801234917238e-03,
|
||||
2.30417297573763929e-05,
|
||||
2.85885980666130812e-04),
|
||||
|
||||
# uranus
|
||||
Planet.new(
|
||||
1.28943695621391310e+01,
|
||||
-1.51111514016986312e+01,
|
||||
-2.23307578892655734e-01,
|
||||
2.96460137564761618e-03,
|
||||
2.37847173959480950e-03,
|
||||
-2.96589568540237556e-05,
|
||||
4.36624404335156298e-05),
|
||||
|
||||
# neptune
|
||||
Planet.new(
|
||||
1.53796971148509165e+01,
|
||||
-2.59193146099879641e+01,
|
||||
1.79258772950371181e-01,
|
||||
2.68067772490389322e-03,
|
||||
1.62824170038242295e-03,
|
||||
-9.51592254519715870e-05,
|
||||
5.15138902046611451e-05)
|
||||
]
|
||||
|
||||
init = 200_000 # ARGV[0]
|
||||
n = Integer(init)
|
||||
|
||||
offset_momentum(BODIES)
|
||||
|
||||
puts "%.9f" % energy(BODIES)
|
||||
|
||||
nbodies = BODIES.size
|
||||
dt = 0.01
|
||||
|
||||
n.times do
|
||||
i = 0
|
||||
while i < nbodies
|
||||
b = BODIES[i]
|
||||
b.move_from_i(BODIES, nbodies, dt, i + 1)
|
||||
i += 1
|
||||
end
|
||||
end
|
||||
|
||||
puts "%.9f" % energy(BODIES)
|
||||
# The Computer Language Shootout
|
||||
# http://shootout.alioth.debian.org
|
||||
#
|
||||
# Optimized for Ruby by Jesse Millikan
|
||||
# From version ported by Michael Neumann from the C gcc version,
|
||||
# which was written by Christoph Bauer.
|
||||
|
||||
SOLAR_MASS = 4 * Math::PI**2
|
||||
DAYS_PER_YEAR = 365.24
|
||||
|
||||
def _puts *args
|
||||
end
|
||||
|
||||
class Planet
|
||||
attr_accessor :x, :y, :z, :vx, :vy, :vz, :mass
|
||||
|
||||
def initialize(x, y, z, vx, vy, vz, mass)
|
||||
@x, @y, @z = x, y, z
|
||||
@vx, @vy, @vz = vx * DAYS_PER_YEAR, vy * DAYS_PER_YEAR, vz * DAYS_PER_YEAR
|
||||
@mass = mass * SOLAR_MASS
|
||||
end
|
||||
|
||||
def move_from_i(bodies, nbodies, dt, i)
|
||||
while i < nbodies
|
||||
b2 = bodies[i]
|
||||
dx = @x - b2.x
|
||||
dy = @y - b2.y
|
||||
dz = @z - b2.z
|
||||
|
||||
distance = Math.sqrt(dx * dx + dy * dy + dz * dz)
|
||||
mag = dt / (distance * distance * distance)
|
||||
b_mass_mag, b2_mass_mag = @mass * mag, b2.mass * mag
|
||||
|
||||
@vx -= dx * b2_mass_mag
|
||||
@vy -= dy * b2_mass_mag
|
||||
@vz -= dz * b2_mass_mag
|
||||
b2.vx += dx * b_mass_mag
|
||||
b2.vy += dy * b_mass_mag
|
||||
b2.vz += dz * b_mass_mag
|
||||
i += 1
|
||||
end
|
||||
|
||||
@x += dt * @vx
|
||||
@y += dt * @vy
|
||||
@z += dt * @vz
|
||||
end
|
||||
end
|
||||
|
||||
def energy(bodies)
|
||||
e = 0.0
|
||||
nbodies = bodies.size
|
||||
|
||||
for i in 0 ... nbodies
|
||||
b = bodies[i]
|
||||
e += 0.5 * b.mass * (b.vx * b.vx + b.vy * b.vy + b.vz * b.vz)
|
||||
for j in (i + 1) ... nbodies
|
||||
b2 = bodies[j]
|
||||
dx = b.x - b2.x
|
||||
dy = b.y - b2.y
|
||||
dz = b.z - b2.z
|
||||
distance = Math.sqrt(dx * dx + dy * dy + dz * dz)
|
||||
e -= (b.mass * b2.mass) / distance
|
||||
end
|
||||
end
|
||||
e
|
||||
end
|
||||
|
||||
def offset_momentum(bodies)
|
||||
px, py, pz = 0.0, 0.0, 0.0
|
||||
|
||||
for b in bodies
|
||||
m = b.mass
|
||||
px += b.vx * m
|
||||
py += b.vy * m
|
||||
pz += b.vz * m
|
||||
end
|
||||
|
||||
b = bodies[0]
|
||||
b.vx = - px / SOLAR_MASS
|
||||
b.vy = - py / SOLAR_MASS
|
||||
b.vz = - pz / SOLAR_MASS
|
||||
end
|
||||
|
||||
BODIES = [
|
||||
# sun
|
||||
Planet.new(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0),
|
||||
|
||||
# jupiter
|
||||
Planet.new(
|
||||
4.84143144246472090e+00,
|
||||
-1.16032004402742839e+00,
|
||||
-1.03622044471123109e-01,
|
||||
1.66007664274403694e-03,
|
||||
7.69901118419740425e-03,
|
||||
-6.90460016972063023e-05,
|
||||
9.54791938424326609e-04),
|
||||
|
||||
# saturn
|
||||
Planet.new(
|
||||
8.34336671824457987e+00,
|
||||
4.12479856412430479e+00,
|
||||
-4.03523417114321381e-01,
|
||||
-2.76742510726862411e-03,
|
||||
4.99852801234917238e-03,
|
||||
2.30417297573763929e-05,
|
||||
2.85885980666130812e-04),
|
||||
|
||||
# uranus
|
||||
Planet.new(
|
||||
1.28943695621391310e+01,
|
||||
-1.51111514016986312e+01,
|
||||
-2.23307578892655734e-01,
|
||||
2.96460137564761618e-03,
|
||||
2.37847173959480950e-03,
|
||||
-2.96589568540237556e-05,
|
||||
4.36624404335156298e-05),
|
||||
|
||||
# neptune
|
||||
Planet.new(
|
||||
1.53796971148509165e+01,
|
||||
-2.59193146099879641e+01,
|
||||
1.79258772950371181e-01,
|
||||
2.68067772490389322e-03,
|
||||
1.62824170038242295e-03,
|
||||
-9.51592254519715870e-05,
|
||||
5.15138902046611451e-05)
|
||||
]
|
||||
|
||||
init = 200_000 # ARGV[0]
|
||||
n = Integer(init)
|
||||
|
||||
offset_momentum(BODIES)
|
||||
|
||||
puts "%.9f" % energy(BODIES)
|
||||
|
||||
nbodies = BODIES.size
|
||||
dt = 0.01
|
||||
|
||||
n.times do
|
||||
i = 0
|
||||
while i < nbodies
|
||||
b = BODIES[i]
|
||||
b.move_from_i(BODIES, nbodies, dt, i + 1)
|
||||
i += 1
|
||||
end
|
||||
end
|
||||
|
||||
puts "%.9f" % energy(BODIES)
|
||||
|
|
|
@ -1,35 +1,35 @@
|
|||
# The Computer Language Shootout
|
||||
# http://shootout.alioth.debian.org/
|
||||
#
|
||||
# contributed by Glenn Parker, March 2005
|
||||
# modified by Evan Phoenix, Sept 2006
|
||||
|
||||
def sieve(m)
|
||||
flags = Flags.dup[0,m]
|
||||
count = 0
|
||||
pmax = m - 1
|
||||
p = 2
|
||||
while p <= pmax
|
||||
unless flags[p].zero?
|
||||
count += 1
|
||||
mult = p
|
||||
while mult <= pmax
|
||||
flags[mult] = 0
|
||||
mult += p
|
||||
end
|
||||
end
|
||||
p += 1
|
||||
end
|
||||
count
|
||||
end
|
||||
|
||||
n = 9 # (ARGV[0] || 2).to_i
|
||||
Flags = ("\x1" * ( 2 ** n * 10_000)).unpack("c*")
|
||||
|
||||
n.downto(n-2) do |exponent|
|
||||
break if exponent < 0
|
||||
m = (1 << exponent) * 10_000
|
||||
# m = (2 ** exponent) * 10_000
|
||||
count = sieve(m)
|
||||
printf "Primes up to %8d %8d\n", m, count
|
||||
end
|
||||
# The Computer Language Shootout
|
||||
# http://shootout.alioth.debian.org/
|
||||
#
|
||||
# contributed by Glenn Parker, March 2005
|
||||
# modified by Evan Phoenix, Sept 2006
|
||||
|
||||
def sieve(m)
|
||||
flags = Flags.dup[0,m]
|
||||
count = 0
|
||||
pmax = m - 1
|
||||
p = 2
|
||||
while p <= pmax
|
||||
unless flags[p].zero?
|
||||
count += 1
|
||||
mult = p
|
||||
while mult <= pmax
|
||||
flags[mult] = 0
|
||||
mult += p
|
||||
end
|
||||
end
|
||||
p += 1
|
||||
end
|
||||
count
|
||||
end
|
||||
|
||||
n = 9 # (ARGV[0] || 2).to_i
|
||||
Flags = ("\x1" * ( 2 ** n * 10_000)).unpack("c*")
|
||||
|
||||
n.downto(n-2) do |exponent|
|
||||
break if exponent < 0
|
||||
m = (1 << exponent) * 10_000
|
||||
# m = (2 ** exponent) * 10_000
|
||||
count = sieve(m)
|
||||
printf "Primes up to %8d %8d\n", m, count
|
||||
end
|
||||
|
|
|
@ -1,42 +1,42 @@
|
|||
#!/usr/bin/ruby
|
||||
#
|
||||
# The Great Computer Language Shootout
|
||||
# http://shootout.alioth.debian.org/
|
||||
#
|
||||
# nsieve-bits in Ruby
|
||||
# Contributed by Glenn Parker, March 2005
|
||||
|
||||
CharExponent = 3
|
||||
BitsPerChar = 1 << CharExponent
|
||||
LowMask = BitsPerChar - 1
|
||||
|
||||
def sieve(m)
|
||||
items = "\xFF" * ((m / BitsPerChar) + 1)
|
||||
masks = ""
|
||||
BitsPerChar.times do |b|
|
||||
masks << (1 << b).chr
|
||||
end
|
||||
|
||||
count = 0
|
||||
pmax = m - 1
|
||||
2.step(pmax, 1) do |p|
|
||||
if items[p >> CharExponent][p & LowMask] == 1
|
||||
count += 1
|
||||
p.step(pmax, p) do |mult|
|
||||
a = mult >> CharExponent
|
||||
b = mult & LowMask
|
||||
items[a] -= masks[b] if items[a][b] != 0
|
||||
end
|
||||
end
|
||||
end
|
||||
count
|
||||
end
|
||||
|
||||
n = 9 # (ARGV[0] || 2).to_i
|
||||
n.step(n - 2, -1) do |exponent|
|
||||
break if exponent < 0
|
||||
m = 2 ** exponent * 10_000
|
||||
count = sieve(m)
|
||||
printf "Primes up to %8d %8d\n", m, count
|
||||
end
|
||||
|
||||
#!/usr/bin/ruby
|
||||
#
|
||||
# The Great Computer Language Shootout
|
||||
# http://shootout.alioth.debian.org/
|
||||
#
|
||||
# nsieve-bits in Ruby
|
||||
# Contributed by Glenn Parker, March 2005
|
||||
|
||||
CharExponent = 3
|
||||
BitsPerChar = 1 << CharExponent
|
||||
LowMask = BitsPerChar - 1
|
||||
|
||||
def sieve(m)
|
||||
items = "\xFF" * ((m / BitsPerChar) + 1)
|
||||
masks = ""
|
||||
BitsPerChar.times do |b|
|
||||
masks << (1 << b).chr
|
||||
end
|
||||
|
||||
count = 0
|
||||
pmax = m - 1
|
||||
2.step(pmax, 1) do |p|
|
||||
if items[p >> CharExponent][p & LowMask] == 1
|
||||
count += 1
|
||||
p.step(pmax, p) do |mult|
|
||||
a = mult >> CharExponent
|
||||
b = mult & LowMask
|
||||
items[a] -= masks[b] if items[a][b] != 0
|
||||
end
|
||||
end
|
||||
end
|
||||
count
|
||||
end
|
||||
|
||||
n = 9 # (ARGV[0] || 2).to_i
|
||||
n.step(n - 2, -1) do |exponent|
|
||||
break if exponent < 0
|
||||
m = 2 ** exponent * 10_000
|
||||
count = sieve(m)
|
||||
printf "Primes up to %8d %8d\n", m, count
|
||||
end
|
||||
|
||||
|
|
|
@ -1,31 +1,31 @@
|
|||
n = 2_500_000 # (ARGV.shift || 1).to_i
|
||||
|
||||
alt = 1.0 ; s0 = s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = 0.0
|
||||
|
||||
1.upto(n) do |d|
|
||||
d = d.to_f ; d2 = d * d ; d3 = d2 * d ; ds = Math.sin(d) ; dc = Math.cos(d)
|
||||
|
||||
s0 += (2.0 / 3.0) ** (d - 1.0)
|
||||
s1 += 1.0 / Math.sqrt(d)
|
||||
s2 += 1.0 / (d * (d + 1.0))
|
||||
s3 += 1.0 / (d3 * ds * ds)
|
||||
s4 += 1.0 / (d3 * dc * dc)
|
||||
s5 += 1.0 / d
|
||||
s6 += 1.0 / d2
|
||||
s7 += alt / d
|
||||
s8 += alt / (2.0 * d - 1.0)
|
||||
|
||||
alt = -alt
|
||||
end
|
||||
|
||||
if false
|
||||
printf("%.9f\t(2/3)^k\n", s0)
|
||||
printf("%.9f\tk^-0.5\n", s1)
|
||||
printf("%.9f\t1/k(k+1)\n", s2)
|
||||
printf("%.9f\tFlint Hills\n", s3)
|
||||
printf("%.9f\tCookson Hills\n", s4)
|
||||
printf("%.9f\tHarmonic\n", s5)
|
||||
printf("%.9f\tRiemann Zeta\n", s6)
|
||||
printf("%.9f\tAlternating Harmonic\n", s7)
|
||||
printf("%.9f\tGregory\n", s8)
|
||||
end
|
||||
n = 2_500_000 # (ARGV.shift || 1).to_i
|
||||
|
||||
alt = 1.0 ; s0 = s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = 0.0
|
||||
|
||||
1.upto(n) do |d|
|
||||
d = d.to_f ; d2 = d * d ; d3 = d2 * d ; ds = Math.sin(d) ; dc = Math.cos(d)
|
||||
|
||||
s0 += (2.0 / 3.0) ** (d - 1.0)
|
||||
s1 += 1.0 / Math.sqrt(d)
|
||||
s2 += 1.0 / (d * (d + 1.0))
|
||||
s3 += 1.0 / (d3 * ds * ds)
|
||||
s4 += 1.0 / (d3 * dc * dc)
|
||||
s5 += 1.0 / d
|
||||
s6 += 1.0 / d2
|
||||
s7 += alt / d
|
||||
s8 += alt / (2.0 * d - 1.0)
|
||||
|
||||
alt = -alt
|
||||
end
|
||||
|
||||
if false
|
||||
printf("%.9f\t(2/3)^k\n", s0)
|
||||
printf("%.9f\tk^-0.5\n", s1)
|
||||
printf("%.9f\t1/k(k+1)\n", s2)
|
||||
printf("%.9f\tFlint Hills\n", s3)
|
||||
printf("%.9f\tCookson Hills\n", s4)
|
||||
printf("%.9f\tHarmonic\n", s5)
|
||||
printf("%.9f\tRiemann Zeta\n", s6)
|
||||
printf("%.9f\tAlternating Harmonic\n", s7)
|
||||
printf("%.9f\tGregory\n", s8)
|
||||
end
|
||||
|
|
|
@ -1,92 +1,92 @@
|
|||
# The Great Computer Language Shootout
|
||||
# http://shootout.alioth.debian.org/
|
||||
#
|
||||
# contributed by Gabriele Renzi
|
||||
|
||||
class PiDigitSpigot
|
||||
|
||||
def initialize()
|
||||
@z = Transformation.new 1,0,0,1
|
||||
@x = Transformation.new 0,0,0,0
|
||||
@inverse = Transformation.new 0,0,0,0
|
||||
end
|
||||
|
||||
def next!
|
||||
@y = @z.extract(3)
|
||||
if safe? @y
|
||||
@z = produce(@y)
|
||||
@y
|
||||
else
|
||||
@z = consume @x.next!()
|
||||
next!()
|
||||
end
|
||||
end
|
||||
|
||||
def safe?(digit)
|
||||
digit == @z.extract(4)
|
||||
end
|
||||
|
||||
def produce(i)
|
||||
@inverse.qrst(10,-10*i,0,1).compose(@z)
|
||||
end
|
||||
|
||||
def consume(a)
|
||||
@z.compose(a)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
class Transformation
|
||||
attr_reader :q, :r, :s, :t
|
||||
def initialize (q, r, s, t)
|
||||
@q,@r,@s,@t,@k = q,r,s,t,0
|
||||
end
|
||||
|
||||
def next!()
|
||||
@q = @k = @k + 1
|
||||
@r = 4 * @k + 2
|
||||
@s = 0
|
||||
@t = 2 * @k + 1
|
||||
self
|
||||
end
|
||||
|
||||
def extract(j)
|
||||
(@q * j + @r) / (@s * j + @t)
|
||||
end
|
||||
|
||||
def compose(a)
|
||||
self.class.new( @q * a.q,
|
||||
@q * a.r + r * a.t,
|
||||
@s * a.q + t * a.s,
|
||||
@s * a.r + t * a.t
|
||||
)
|
||||
end
|
||||
|
||||
def qrst *args
|
||||
initialize *args
|
||||
self
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
||||
WIDTH = 10
|
||||
n = 2_500 # Integer(ARGV[0])
|
||||
j = 0
|
||||
|
||||
digits = PiDigitSpigot.new
|
||||
|
||||
while n > 0
|
||||
if n >= WIDTH
|
||||
WIDTH.times {print digits.next!}
|
||||
j += WIDTH
|
||||
else
|
||||
n.times {print digits.next!}
|
||||
(WIDTH-n).times {print " "}
|
||||
j += n
|
||||
end
|
||||
puts "\t:"+j.to_s
|
||||
n -= WIDTH
|
||||
end
|
||||
|
||||
# The Great Computer Language Shootout
|
||||
# http://shootout.alioth.debian.org/
|
||||
#
|
||||
# contributed by Gabriele Renzi
|
||||
|
||||
class PiDigitSpigot
|
||||
|
||||
def initialize()
|
||||
@z = Transformation.new 1,0,0,1
|
||||
@x = Transformation.new 0,0,0,0
|
||||
@inverse = Transformation.new 0,0,0,0
|
||||
end
|
||||
|
||||
def next!
|
||||
@y = @z.extract(3)
|
||||
if safe? @y
|
||||
@z = produce(@y)
|
||||
@y
|
||||
else
|
||||
@z = consume @x.next!()
|
||||
next!()
|
||||
end
|
||||
end
|
||||
|
||||
def safe?(digit)
|
||||
digit == @z.extract(4)
|
||||
end
|
||||
|
||||
def produce(i)
|
||||
@inverse.qrst(10,-10*i,0,1).compose(@z)
|
||||
end
|
||||
|
||||
def consume(a)
|
||||
@z.compose(a)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
class Transformation
|
||||
attr_reader :q, :r, :s, :t
|
||||
def initialize (q, r, s, t)
|
||||
@q,@r,@s,@t,@k = q,r,s,t,0
|
||||
end
|
||||
|
||||
def next!()
|
||||
@q = @k = @k + 1
|
||||
@r = 4 * @k + 2
|
||||
@s = 0
|
||||
@t = 2 * @k + 1
|
||||
self
|
||||
end
|
||||
|
||||
def extract(j)
|
||||
(@q * j + @r) / (@s * j + @t)
|
||||
end
|
||||
|
||||
def compose(a)
|
||||
self.class.new( @q * a.q,
|
||||
@q * a.r + r * a.t,
|
||||
@s * a.q + t * a.s,
|
||||
@s * a.r + t * a.t
|
||||
)
|
||||
end
|
||||
|
||||
def qrst *args
|
||||
initialize *args
|
||||
self
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
||||
WIDTH = 10
|
||||
n = 2_500 # Integer(ARGV[0])
|
||||
j = 0
|
||||
|
||||
digits = PiDigitSpigot.new
|
||||
|
||||
while n > 0
|
||||
if n >= WIDTH
|
||||
WIDTH.times {print digits.next!}
|
||||
j += WIDTH
|
||||
else
|
||||
n.times {print digits.next!}
|
||||
(WIDTH-n).times {print " "}
|
||||
j += n
|
||||
end
|
||||
puts "\t:"+j.to_s
|
||||
n -= WIDTH
|
||||
end
|
||||
|
||||
|
|
|
@ -1,30 +1,30 @@
|
|||
#!/usr/bin/ruby
|
||||
# The Great Computer Language Shootout
|
||||
# http://shootout.alioth.debian.org/
|
||||
#
|
||||
# Contributed by Peter Bjarke Olsen
|
||||
# Modified by Doug King
|
||||
|
||||
seq=Array.new
|
||||
|
||||
def revcomp(seq)
|
||||
seq.reverse!.tr!('wsatugcyrkmbdhvnATUGCYRKMBDHVN','WSTAACGRYMKVHDBNTAACGRYMKVHDBN')
|
||||
stringlen=seq.length
|
||||
0.step(stringlen-1,60) {|x| print seq.slice(x,60) , "\n"}
|
||||
end
|
||||
|
||||
input = open(File.join(File.dirname($0), 'fasta.output.2500000'), 'rb')
|
||||
|
||||
while input.gets
|
||||
if $_ =~ />/
|
||||
if seq.length != 0
|
||||
revcomp(seq.join)
|
||||
seq=Array.new
|
||||
end
|
||||
puts $_
|
||||
else
|
||||
$_.sub(/\n/,'')
|
||||
seq.push $_
|
||||
end
|
||||
end
|
||||
revcomp(seq.join)
|
||||
#!/usr/bin/ruby
|
||||
# The Great Computer Language Shootout
|
||||
# http://shootout.alioth.debian.org/
|
||||
#
|
||||
# Contributed by Peter Bjarke Olsen
|
||||
# Modified by Doug King
|
||||
|
||||
seq=Array.new
|
||||
|
||||
def revcomp(seq)
|
||||
seq.reverse!.tr!('wsatugcyrkmbdhvnATUGCYRKMBDHVN','WSTAACGRYMKVHDBNTAACGRYMKVHDBN')
|
||||
stringlen=seq.length
|
||||
0.step(stringlen-1,60) {|x| print seq.slice(x,60) , "\n"}
|
||||
end
|
||||
|
||||
input = open(File.join(File.dirname($0), 'fasta.output.2500000'), 'rb')
|
||||
|
||||
while input.gets
|
||||
if $_ =~ />/
|
||||
if seq.length != 0
|
||||
revcomp(seq.join)
|
||||
seq=Array.new
|
||||
end
|
||||
puts $_
|
||||
else
|
||||
$_.sub(/\n/,'')
|
||||
seq.push $_
|
||||
end
|
||||
end
|
||||
revcomp(seq.join)
|
||||
|
|
|
@ -1,50 +1,50 @@
|
|||
# The Computer Language Shootout
|
||||
# http://shootout.alioth.debian.org/
|
||||
# Contributed by Sokolov Yura
|
||||
|
||||
def eval_A(i,j)
|
||||
return 1.0/((i+j)*(i+j+1)/2+i+1)
|
||||
end
|
||||
|
||||
def eval_A_times_u(u)
|
||||
v, i = nil, nil
|
||||
(0..u.length-1).collect { |i|
|
||||
v = 0
|
||||
for j in 0..u.length-1
|
||||
v += eval_A(i,j)*u[j]
|
||||
end
|
||||
v
|
||||
}
|
||||
end
|
||||
|
||||
def eval_At_times_u(u)
|
||||
v, i = nil, nil
|
||||
(0..u.length-1).collect{|i|
|
||||
v = 0
|
||||
for j in 0..u.length-1
|
||||
v += eval_A(j,i)*u[j]
|
||||
end
|
||||
v
|
||||
}
|
||||
end
|
||||
|
||||
def eval_AtA_times_u(u)
|
||||
return eval_At_times_u(eval_A_times_u(u))
|
||||
end
|
||||
|
||||
n = 500 # ARGV[0].to_i
|
||||
|
||||
u=[1]*n
|
||||
for i in 1..10
|
||||
v=eval_AtA_times_u(u)
|
||||
u=eval_AtA_times_u(v)
|
||||
end
|
||||
vBv=0
|
||||
vv=0
|
||||
for i in 0..n-1
|
||||
vBv += u[i]*v[i]
|
||||
vv += v[i]*v[i]
|
||||
end
|
||||
|
||||
str = "%0.9f" % (Math.sqrt(vBv/vv)), "\n"
|
||||
# print str
|
||||
# The Computer Language Shootout
|
||||
# http://shootout.alioth.debian.org/
|
||||
# Contributed by Sokolov Yura
|
||||
|
||||
def eval_A(i,j)
|
||||
return 1.0/((i+j)*(i+j+1)/2+i+1)
|
||||
end
|
||||
|
||||
def eval_A_times_u(u)
|
||||
v, i = nil, nil
|
||||
(0..u.length-1).collect { |i|
|
||||
v = 0
|
||||
for j in 0..u.length-1
|
||||
v += eval_A(i,j)*u[j]
|
||||
end
|
||||
v
|
||||
}
|
||||
end
|
||||
|
||||
def eval_At_times_u(u)
|
||||
v, i = nil, nil
|
||||
(0..u.length-1).collect{|i|
|
||||
v = 0
|
||||
for j in 0..u.length-1
|
||||
v += eval_A(j,i)*u[j]
|
||||
end
|
||||
v
|
||||
}
|
||||
end
|
||||
|
||||
def eval_AtA_times_u(u)
|
||||
return eval_At_times_u(eval_A_times_u(u))
|
||||
end
|
||||
|
||||
n = 500 # ARGV[0].to_i
|
||||
|
||||
u=[1]*n
|
||||
for i in 1..10
|
||||
v=eval_AtA_times_u(u)
|
||||
u=eval_AtA_times_u(v)
|
||||
end
|
||||
vBv=0
|
||||
vv=0
|
||||
for i in 0..n-1
|
||||
vBv += u[i]*v[i]
|
||||
vv += v[i]*v[i]
|
||||
end
|
||||
|
||||
str = "%0.9f" % (Math.sqrt(vBv/vv)), "\n"
|
||||
# print str
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
i = 0
|
||||
while i<30_000_000 # while loop 1
|
||||
i+= 1
|
||||
@a = 1
|
||||
@b = 2
|
||||
end
|
||||
i = 0
|
||||
while i<30_000_000 # while loop 1
|
||||
i+= 1
|
||||
@a = 1
|
||||
@b = 2
|
||||
end
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
i=0
|
||||
while i<6000000 # benchmark loop 2
|
||||
i+=1
|
||||
eval("1")
|
||||
end
|
||||
|
||||
i=0
|
||||
while i<6000000 # benchmark loop 2
|
||||
i+=1
|
||||
eval("1")
|
||||
end
|
||||
|
||||
|
|
|
@ -1,238 +1,238 @@
|
|||
#
|
||||
# Ruby Benchmark driver
|
||||
#
|
||||
|
||||
require 'optparse'
|
||||
require 'benchmark'
|
||||
require 'pp'
|
||||
|
||||
class BenchmarkDriver
|
||||
def self.benchmark(opt)
|
||||
driver = self.new(opt[:execs], opt[:dir], opt)
|
||||
begin
|
||||
driver.run
|
||||
ensure
|
||||
driver.show_results
|
||||
end
|
||||
end
|
||||
|
||||
def output *args
|
||||
puts(*args)
|
||||
@output and @output.puts(*args)
|
||||
end
|
||||
|
||||
def message *args
|
||||
output(*args) if @verbose
|
||||
end
|
||||
|
||||
def message_print *args
|
||||
if @verbose
|
||||
print(*args)
|
||||
STDOUT.flush
|
||||
@output and @output.print(*args)
|
||||
end
|
||||
end
|
||||
|
||||
def progress_message *args
|
||||
unless STDOUT.tty?
|
||||
STDERR.print(*args)
|
||||
STDERR.flush
|
||||
end
|
||||
end
|
||||
|
||||
def initialize execs, dir, opt = {}
|
||||
@execs = execs.map{|e|
|
||||
e.strip!
|
||||
next if e.empty?
|
||||
|
||||
if /(.+)::(.+)/ =~ e
|
||||
# ex) ruby-a::/path/to/ruby-a
|
||||
v = $1.strip
|
||||
e = $2
|
||||
else
|
||||
v = `#{e} -v`.chomp
|
||||
v.sub!(/ patchlevel \d+/, '')
|
||||
end
|
||||
[e, v]
|
||||
}.compact
|
||||
|
||||
@dir = dir
|
||||
@repeat = opt[:repeat] || 1
|
||||
@repeat = 1 if @repeat < 1
|
||||
@pattern = opt[:pattern] || nil
|
||||
@verbose = opt[:quiet] ? false : (opt[:verbose] || false)
|
||||
@output = opt[:output] ? open(opt[:output], 'w') : nil
|
||||
@loop_wl1 = @loop_wl2 = nil
|
||||
@opt = opt
|
||||
|
||||
# [[name, [[r-1-1, r-1-2, ...], [r-2-1, r-2-2, ...]]], ...]
|
||||
@results = []
|
||||
|
||||
if @verbose
|
||||
@start_time = Time.now
|
||||
message @start_time
|
||||
@execs.each_with_index{|(e, v), i|
|
||||
message "target #{i}: #{v}"
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def show_results
|
||||
output
|
||||
|
||||
if @verbose
|
||||
message '-----------------------------------------------------------'
|
||||
message 'raw data:'
|
||||
message
|
||||
message PP.pp(@results, "", 79)
|
||||
message
|
||||
message "Elapesed time: #{Time.now - @start_time} (sec)"
|
||||
end
|
||||
|
||||
output '-----------------------------------------------------------'
|
||||
output 'benchmark results:'
|
||||
|
||||
if @verbose and @repeat > 1
|
||||
output "minimum results in each #{@repeat} measurements."
|
||||
end
|
||||
|
||||
output "name\t#{@execs.map{|(e, v)| v}.join("\t")}"
|
||||
@results.each{|v, result|
|
||||
rets = []
|
||||
s = nil
|
||||
result.each_with_index{|e, i|
|
||||
r = e.min
|
||||
case v
|
||||
when /^vm1_/
|
||||
if @loop_wl1
|
||||
r -= @loop_wl1[i]
|
||||
s = '*'
|
||||
end
|
||||
when /^vm2_/
|
||||
if @loop_wl2
|
||||
r -= @loop_wl2[i]
|
||||
s = '*'
|
||||
end
|
||||
end
|
||||
rets << sprintf("%.3f", r)
|
||||
}
|
||||
output "#{v}#{s}\t#{rets.join("\t")}"
|
||||
}
|
||||
end
|
||||
|
||||
def files
|
||||
flag = {}
|
||||
vm1 = vm2 = wl1 = wl2 = false
|
||||
@files = Dir.glob(File.join(@dir, 'bm*.rb')).map{|file|
|
||||
next if @pattern && /#{@pattern}/ !~ File.basename(file)
|
||||
case file
|
||||
when /bm_(vm[12])_/, /bm_loop_(whileloop2?).rb/
|
||||
flag[$1] = true
|
||||
end
|
||||
file
|
||||
}.compact
|
||||
|
||||
if flag['vm1'] && !flag['whileloop']
|
||||
@files << File.join(@dir, 'bm_loop_whileloop.rb')
|
||||
elsif flag['vm2'] && !flag['whileloop2']
|
||||
@files << File.join(@dir, 'bm_loop_whileloop2.rb')
|
||||
end
|
||||
|
||||
@files.sort!
|
||||
progress_message "total: #{@files.size * @repeat} trial(s) (#{@repeat} trial(s) for #{@files.size} benchmark(s))\n"
|
||||
@files
|
||||
end
|
||||
|
||||
def run
|
||||
files.each_with_index{|file, i|
|
||||
@i = i
|
||||
r = measure_file(file)
|
||||
|
||||
if /bm_loop_whileloop.rb/ =~ file
|
||||
@loop_wl1 = r[1].map{|e| e.min}
|
||||
elsif /bm_loop_whileloop2.rb/ =~ file
|
||||
@loop_wl2 = r[1].map{|e| e.min}
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
def measure_file file
|
||||
name = File.basename(file, '.rb').sub(/^bm_/, '')
|
||||
prepare_file = File.join(File.dirname(file), "prepare_#{name}.rb")
|
||||
load prepare_file if FileTest.exist?(prepare_file)
|
||||
|
||||
if @verbose
|
||||
output
|
||||
output '-----------------------------------------------------------'
|
||||
output name
|
||||
output
|
||||
output File.read(file)
|
||||
output
|
||||
end
|
||||
|
||||
result = [name]
|
||||
result << @execs.map{|(e, v)|
|
||||
(0...@repeat).map{
|
||||
message_print "#{v}\t"
|
||||
progress_message '.'
|
||||
|
||||
m = measure(e, file)
|
||||
message "#{m}"
|
||||
m
|
||||
}
|
||||
}
|
||||
@results << result
|
||||
result
|
||||
end
|
||||
|
||||
def measure executable, file
|
||||
cmd = "#{executable} #{file}"
|
||||
m = Benchmark.measure{
|
||||
`#{cmd}`
|
||||
}
|
||||
|
||||
if $? != 0
|
||||
raise "Benchmark process exited with abnormal status (#{$?})"
|
||||
end
|
||||
|
||||
m.real
|
||||
end
|
||||
end
|
||||
|
||||
if __FILE__ == $0
|
||||
opt = {
|
||||
:execs => ['ruby'],
|
||||
:dir => './',
|
||||
:repeat => 1,
|
||||
:output => "bmlog-#{Time.now.strftime('%Y%m%d-%H%M%S')}.#{$$}",
|
||||
}
|
||||
|
||||
parser = OptionParser.new{|o|
|
||||
o.on('-e', '--executables [EXECS]',
|
||||
"Specify benchmark one or more targets. (exec1; exec2; exec3, ...)"){|e|
|
||||
opt[:execs] = e.split(/;/)
|
||||
}
|
||||
o.on('-d', '--directory [DIRECTORY]', "Benchmark suites directory"){|d|
|
||||
opt[:dir] = d
|
||||
}
|
||||
o.on('-p', '--pattern [PATTERN]', "Benchmark name pattern"){|p|
|
||||
opt[:pattern] = p
|
||||
}
|
||||
o.on('-r', '--repeat-count [NUM]', "Repeat count"){|n|
|
||||
opt[:repeat] = n.to_i
|
||||
}
|
||||
o.on('-o', '--output-file [FILE]', "Output file"){|o|
|
||||
opt[:output] = o
|
||||
}
|
||||
o.on('-q', '--quiet', "Run without notify information except result table."){|q|
|
||||
opt[:quiet] = q
|
||||
}
|
||||
o.on('-v', '--verbose'){|v|
|
||||
opt[:verbose] = v
|
||||
}
|
||||
}
|
||||
|
||||
parser.parse!(ARGV)
|
||||
BenchmarkDriver.benchmark(opt)
|
||||
end
|
||||
|
||||
#
|
||||
# Ruby Benchmark driver
|
||||
#
|
||||
|
||||
require 'optparse'
|
||||
require 'benchmark'
|
||||
require 'pp'
|
||||
|
||||
class BenchmarkDriver
|
||||
def self.benchmark(opt)
|
||||
driver = self.new(opt[:execs], opt[:dir], opt)
|
||||
begin
|
||||
driver.run
|
||||
ensure
|
||||
driver.show_results
|
||||
end
|
||||
end
|
||||
|
||||
def output *args
|
||||
puts(*args)
|
||||
@output and @output.puts(*args)
|
||||
end
|
||||
|
||||
def message *args
|
||||
output(*args) if @verbose
|
||||
end
|
||||
|
||||
def message_print *args
|
||||
if @verbose
|
||||
print(*args)
|
||||
STDOUT.flush
|
||||
@output and @output.print(*args)
|
||||
end
|
||||
end
|
||||
|
||||
def progress_message *args
|
||||
unless STDOUT.tty?
|
||||
STDERR.print(*args)
|
||||
STDERR.flush
|
||||
end
|
||||
end
|
||||
|
||||
def initialize execs, dir, opt = {}
|
||||
@execs = execs.map{|e|
|
||||
e.strip!
|
||||
next if e.empty?
|
||||
|
||||
if /(.+)::(.+)/ =~ e
|
||||
# ex) ruby-a::/path/to/ruby-a
|
||||
v = $1.strip
|
||||
e = $2
|
||||
else
|
||||
v = `#{e} -v`.chomp
|
||||
v.sub!(/ patchlevel \d+/, '')
|
||||
end
|
||||
[e, v]
|
||||
}.compact
|
||||
|
||||
@dir = dir
|
||||
@repeat = opt[:repeat] || 1
|
||||
@repeat = 1 if @repeat < 1
|
||||
@pattern = opt[:pattern] || nil
|
||||
@verbose = opt[:quiet] ? false : (opt[:verbose] || false)
|
||||
@output = opt[:output] ? open(opt[:output], 'w') : nil
|
||||
@loop_wl1 = @loop_wl2 = nil
|
||||
@opt = opt
|
||||
|
||||
# [[name, [[r-1-1, r-1-2, ...], [r-2-1, r-2-2, ...]]], ...]
|
||||
@results = []
|
||||
|
||||
if @verbose
|
||||
@start_time = Time.now
|
||||
message @start_time
|
||||
@execs.each_with_index{|(e, v), i|
|
||||
message "target #{i}: #{v}"
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def show_results
|
||||
output
|
||||
|
||||
if @verbose
|
||||
message '-----------------------------------------------------------'
|
||||
message 'raw data:'
|
||||
message
|
||||
message PP.pp(@results, "", 79)
|
||||
message
|
||||
message "Elapesed time: #{Time.now - @start_time} (sec)"
|
||||
end
|
||||
|
||||
output '-----------------------------------------------------------'
|
||||
output 'benchmark results:'
|
||||
|
||||
if @verbose and @repeat > 1
|
||||
output "minimum results in each #{@repeat} measurements."
|
||||
end
|
||||
|
||||
output "name\t#{@execs.map{|(e, v)| v}.join("\t")}"
|
||||
@results.each{|v, result|
|
||||
rets = []
|
||||
s = nil
|
||||
result.each_with_index{|e, i|
|
||||
r = e.min
|
||||
case v
|
||||
when /^vm1_/
|
||||
if @loop_wl1
|
||||
r -= @loop_wl1[i]
|
||||
s = '*'
|
||||
end
|
||||
when /^vm2_/
|
||||
if @loop_wl2
|
||||
r -= @loop_wl2[i]
|
||||
s = '*'
|
||||
end
|
||||
end
|
||||
rets << sprintf("%.3f", r)
|
||||
}
|
||||
output "#{v}#{s}\t#{rets.join("\t")}"
|
||||
}
|
||||
end
|
||||
|
||||
def files
|
||||
flag = {}
|
||||
vm1 = vm2 = wl1 = wl2 = false
|
||||
@files = Dir.glob(File.join(@dir, 'bm*.rb')).map{|file|
|
||||
next if @pattern && /#{@pattern}/ !~ File.basename(file)
|
||||
case file
|
||||
when /bm_(vm[12])_/, /bm_loop_(whileloop2?).rb/
|
||||
flag[$1] = true
|
||||
end
|
||||
file
|
||||
}.compact
|
||||
|
||||
if flag['vm1'] && !flag['whileloop']
|
||||
@files << File.join(@dir, 'bm_loop_whileloop.rb')
|
||||
elsif flag['vm2'] && !flag['whileloop2']
|
||||
@files << File.join(@dir, 'bm_loop_whileloop2.rb')
|
||||
end
|
||||
|
||||
@files.sort!
|
||||
progress_message "total: #{@files.size * @repeat} trial(s) (#{@repeat} trial(s) for #{@files.size} benchmark(s))\n"
|
||||
@files
|
||||
end
|
||||
|
||||
def run
|
||||
files.each_with_index{|file, i|
|
||||
@i = i
|
||||
r = measure_file(file)
|
||||
|
||||
if /bm_loop_whileloop.rb/ =~ file
|
||||
@loop_wl1 = r[1].map{|e| e.min}
|
||||
elsif /bm_loop_whileloop2.rb/ =~ file
|
||||
@loop_wl2 = r[1].map{|e| e.min}
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
def measure_file file
|
||||
name = File.basename(file, '.rb').sub(/^bm_/, '')
|
||||
prepare_file = File.join(File.dirname(file), "prepare_#{name}.rb")
|
||||
load prepare_file if FileTest.exist?(prepare_file)
|
||||
|
||||
if @verbose
|
||||
output
|
||||
output '-----------------------------------------------------------'
|
||||
output name
|
||||
output
|
||||
output File.read(file)
|
||||
output
|
||||
end
|
||||
|
||||
result = [name]
|
||||
result << @execs.map{|(e, v)|
|
||||
(0...@repeat).map{
|
||||
message_print "#{v}\t"
|
||||
progress_message '.'
|
||||
|
||||
m = measure(e, file)
|
||||
message "#{m}"
|
||||
m
|
||||
}
|
||||
}
|
||||
@results << result
|
||||
result
|
||||
end
|
||||
|
||||
def measure executable, file
|
||||
cmd = "#{executable} #{file}"
|
||||
m = Benchmark.measure{
|
||||
`#{cmd}`
|
||||
}
|
||||
|
||||
if $? != 0
|
||||
raise "Benchmark process exited with abnormal status (#{$?})"
|
||||
end
|
||||
|
||||
m.real
|
||||
end
|
||||
end
|
||||
|
||||
if __FILE__ == $0
|
||||
opt = {
|
||||
:execs => ['ruby'],
|
||||
:dir => './',
|
||||
:repeat => 1,
|
||||
:output => "bmlog-#{Time.now.strftime('%Y%m%d-%H%M%S')}.#{$$}",
|
||||
}
|
||||
|
||||
parser = OptionParser.new{|o|
|
||||
o.on('-e', '--executables [EXECS]',
|
||||
"Specify benchmark one or more targets. (exec1; exec2; exec3, ...)"){|e|
|
||||
opt[:execs] = e.split(/;/)
|
||||
}
|
||||
o.on('-d', '--directory [DIRECTORY]', "Benchmark suites directory"){|d|
|
||||
opt[:dir] = d
|
||||
}
|
||||
o.on('-p', '--pattern [PATTERN]', "Benchmark name pattern"){|p|
|
||||
opt[:pattern] = p
|
||||
}
|
||||
o.on('-r', '--repeat-count [NUM]', "Repeat count"){|n|
|
||||
opt[:repeat] = n.to_i
|
||||
}
|
||||
o.on('-o', '--output-file [FILE]', "Output file"){|o|
|
||||
opt[:output] = o
|
||||
}
|
||||
o.on('-q', '--quiet', "Run without notify information except result table."){|q|
|
||||
opt[:quiet] = q
|
||||
}
|
||||
o.on('-v', '--verbose'){|v|
|
||||
opt[:verbose] = v
|
||||
}
|
||||
}
|
||||
|
||||
parser.parse!(ARGV)
|
||||
BenchmarkDriver.benchmark(opt)
|
||||
end
|
||||
|
||||
|
|
|
@ -1,19 +1,19 @@
|
|||
# prepare 'fasta.output'
|
||||
|
||||
def prepare_fasta_output n
|
||||
filebase = File.join(File.dirname($0), 'fasta.output')
|
||||
script = File.join(File.dirname($0), 'bm_so_fasta.rb')
|
||||
file = "#{filebase}.#{n}"
|
||||
|
||||
unless FileTest.exist?(file)
|
||||
STDERR.puts "preparing #{file}"
|
||||
|
||||
open(file, 'w'){|f|
|
||||
ARGV[0] = n
|
||||
$stdout = f
|
||||
load script
|
||||
$stdout = STDOUT
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
# prepare 'fasta.output'
|
||||
|
||||
def prepare_fasta_output n
|
||||
filebase = File.join(File.dirname($0), 'fasta.output')
|
||||
script = File.join(File.dirname($0), 'bm_so_fasta.rb')
|
||||
file = "#{filebase}.#{n}"
|
||||
|
||||
unless FileTest.exist?(file)
|
||||
STDERR.puts "preparing #{file}"
|
||||
|
||||
open(file, 'w'){|f|
|
||||
ARGV[0] = n
|
||||
$stdout = f
|
||||
load script
|
||||
$stdout = STDOUT
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
# prepare 'wc.input'
|
||||
|
||||
def prepare_wc_input
|
||||
wcinput = File.join(File.dirname($0), 'wc.input')
|
||||
wcbase = File.join(File.dirname($0), 'wc.input.base')
|
||||
unless FileTest.exist?(wcinput)
|
||||
data = File.read(wcbase)
|
||||
13.times{
|
||||
data << data
|
||||
}
|
||||
open(wcinput, 'w'){|f| f.write data}
|
||||
end
|
||||
end
|
||||
|
||||
prepare_wc_input
|
||||
# prepare 'wc.input'
|
||||
|
||||
def prepare_wc_input
|
||||
wcinput = File.join(File.dirname($0), 'wc.input')
|
||||
wcbase = File.join(File.dirname($0), 'wc.input.base')
|
||||
unless FileTest.exist?(wcinput)
|
||||
data = File.read(wcbase)
|
||||
13.times{
|
||||
data << data
|
||||
}
|
||||
open(wcinput, 'w'){|f| f.write data}
|
||||
end
|
||||
end
|
||||
|
||||
prepare_wc_input
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
require File.join(File.dirname(__FILE__), 'make_fasta_output')
|
||||
prepare_fasta_output(100_000)
|
||||
require File.join(File.dirname(__FILE__), 'make_fasta_output')
|
||||
prepare_fasta_output(100_000)
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
require File.join(File.dirname(__FILE__), 'make_fasta_output')
|
||||
prepare_fasta_output(2_500_000)
|
||||
require File.join(File.dirname(__FILE__), 'make_fasta_output')
|
||||
prepare_fasta_output(2_500_000)
|
||||
|
|
106
id.h
106
id.h
|
@ -1,53 +1,53 @@
|
|||
/**********************************************************************
|
||||
|
||||
id.h -
|
||||
|
||||
$Author: ko1 $
|
||||
$Date: $
|
||||
created at: Thu Jul 12 04:38:07 2007
|
||||
|
||||
Copyright (C) 2007 Koichi Sasada
|
||||
|
||||
**********************************************************************/
|
||||
|
||||
extern VALUE symIFUNC;
|
||||
extern VALUE symCFUNC;
|
||||
|
||||
extern ID idPLUS;
|
||||
extern ID idMINUS;
|
||||
extern ID idMULT;
|
||||
extern ID idDIV;
|
||||
extern ID idMOD;
|
||||
extern ID idLT;
|
||||
extern ID idLTLT;
|
||||
extern ID idLE;
|
||||
extern ID idGT;
|
||||
extern ID idGE;
|
||||
extern ID idEq;
|
||||
extern ID idEqq;
|
||||
extern ID idBackquote;
|
||||
extern ID idEqTilde;
|
||||
extern ID idThrowState;
|
||||
extern ID idAREF;
|
||||
extern ID idASET;
|
||||
extern ID idIntern;
|
||||
extern ID idMethodMissing;
|
||||
extern ID idLength;
|
||||
extern ID idGets;
|
||||
extern ID idSucc;
|
||||
extern ID idEach;
|
||||
extern ID idLambda;
|
||||
extern ID idRangeEachLT;
|
||||
extern ID idRangeEachLE;
|
||||
extern ID idArrayEach;
|
||||
extern ID idTimes;
|
||||
extern ID idEnd;
|
||||
extern ID idBitblt;
|
||||
extern ID idAnswer;
|
||||
extern ID idSend;
|
||||
extern ID idSendBang;
|
||||
extern ID id__send;
|
||||
extern ID id__send_bang;
|
||||
extern ID id__send__;
|
||||
|
||||
|
||||
/**********************************************************************
|
||||
|
||||
id.h -
|
||||
|
||||
$Author: ko1 $
|
||||
$Date: $
|
||||
created at: Thu Jul 12 04:38:07 2007
|
||||
|
||||
Copyright (C) 2007 Koichi Sasada
|
||||
|
||||
**********************************************************************/
|
||||
|
||||
extern VALUE symIFUNC;
|
||||
extern VALUE symCFUNC;
|
||||
|
||||
extern ID idPLUS;
|
||||
extern ID idMINUS;
|
||||
extern ID idMULT;
|
||||
extern ID idDIV;
|
||||
extern ID idMOD;
|
||||
extern ID idLT;
|
||||
extern ID idLTLT;
|
||||
extern ID idLE;
|
||||
extern ID idGT;
|
||||
extern ID idGE;
|
||||
extern ID idEq;
|
||||
extern ID idEqq;
|
||||
extern ID idBackquote;
|
||||
extern ID idEqTilde;
|
||||
extern ID idThrowState;
|
||||
extern ID idAREF;
|
||||
extern ID idASET;
|
||||
extern ID idIntern;
|
||||
extern ID idMethodMissing;
|
||||
extern ID idLength;
|
||||
extern ID idGets;
|
||||
extern ID idSucc;
|
||||
extern ID idEach;
|
||||
extern ID idLambda;
|
||||
extern ID idRangeEachLT;
|
||||
extern ID idRangeEachLE;
|
||||
extern ID idArrayEach;
|
||||
extern ID idTimes;
|
||||
extern ID idEnd;
|
||||
extern ID idBitblt;
|
||||
extern ID idAnswer;
|
||||
extern ID idSend;
|
||||
extern ID idSendBang;
|
||||
extern ID id__send;
|
||||
extern ID id__send_bang;
|
||||
extern ID id__send__;
|
||||
|
||||
|
||||
|
|
|
@ -1,47 +1,47 @@
|
|||
require 'test/unit'
|
||||
|
||||
class TestEnumerator < Test::Unit::TestCase
|
||||
def enum_test obj
|
||||
i = 0
|
||||
obj.map{|e|
|
||||
e
|
||||
}.sort
|
||||
end
|
||||
|
||||
def test_iterators
|
||||
assert_equal [0, 1, 2], enum_test(3.times)
|
||||
assert_equal [:x, :y, :z], enum_test([:x, :y, :z].each)
|
||||
assert_equal [[:x, 1], [:y, 2]], enum_test({:x=>1, :y=>2})
|
||||
end
|
||||
|
||||
## Enumerator as Iterator
|
||||
|
||||
def test_next
|
||||
e = 3.times
|
||||
3.times{|i|
|
||||
assert_equal i, e.next
|
||||
}
|
||||
assert_raise(StopIteration){e.next}
|
||||
end
|
||||
|
||||
def test_loop
|
||||
e = 3.times
|
||||
i = 0
|
||||
loop{
|
||||
assert_equal(i, e.next)
|
||||
i += 1
|
||||
}
|
||||
end
|
||||
|
||||
def test_nested_itaration
|
||||
def (o = Object.new).each
|
||||
yield :ok1
|
||||
yield [:ok2, :x].each.next
|
||||
end
|
||||
e = o.to_enum
|
||||
assert_equal :ok1, e.next
|
||||
assert_equal :ok2, e.next
|
||||
assert_raise(StopIteration){e.next}
|
||||
end
|
||||
end
|
||||
|
||||
require 'test/unit'
|
||||
|
||||
class TestEnumerator < Test::Unit::TestCase
|
||||
def enum_test obj
|
||||
i = 0
|
||||
obj.map{|e|
|
||||
e
|
||||
}.sort
|
||||
end
|
||||
|
||||
def test_iterators
|
||||
assert_equal [0, 1, 2], enum_test(3.times)
|
||||
assert_equal [:x, :y, :z], enum_test([:x, :y, :z].each)
|
||||
assert_equal [[:x, 1], [:y, 2]], enum_test({:x=>1, :y=>2})
|
||||
end
|
||||
|
||||
## Enumerator as Iterator
|
||||
|
||||
def test_next
|
||||
e = 3.times
|
||||
3.times{|i|
|
||||
assert_equal i, e.next
|
||||
}
|
||||
assert_raise(StopIteration){e.next}
|
||||
end
|
||||
|
||||
def test_loop
|
||||
e = 3.times
|
||||
i = 0
|
||||
loop{
|
||||
assert_equal(i, e.next)
|
||||
i += 1
|
||||
}
|
||||
end
|
||||
|
||||
def test_nested_itaration
|
||||
def (o = Object.new).each
|
||||
yield :ok1
|
||||
yield [:ok2, :x].each.next
|
||||
end
|
||||
e = o.to_enum
|
||||
assert_equal :ok1, e.next
|
||||
assert_equal :ok2, e.next
|
||||
assert_raise(StopIteration){e.next}
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче