-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
1,462 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
-- The Computer Language Benchmarks Game | ||
-- http://shootout.alioth.debian.org/ | ||
-- contributed by Mike Pall | ||
|
||
local function BottomUpTree(item, depth) | ||
if depth > 0 then | ||
local i = item + item | ||
depth = depth - 1 | ||
local left, right = BottomUpTree(i-1, depth), BottomUpTree(i, depth) | ||
return { item, left, right } | ||
else | ||
return { item } | ||
end | ||
end | ||
|
||
local function ItemCheck(tree) | ||
if tree[2] then | ||
return tree[1] + ItemCheck(tree[2]) - ItemCheck(tree[3]) | ||
else | ||
return tree[1] | ||
end | ||
end | ||
|
||
local N = 12 | ||
local mindepth = 4 | ||
local maxdepth = mindepth + 2 | ||
if maxdepth < N then maxdepth = N end | ||
|
||
local start = os.clock() | ||
|
||
do | ||
local stretchdepth = maxdepth + 1 | ||
local stretchtree = BottomUpTree(0, stretchdepth) | ||
io.write(string.format("stretch tree of depth %d check: %d\n", | ||
stretchdepth, ItemCheck(stretchtree))) | ||
end | ||
|
||
local longlivedtree = BottomUpTree(0, maxdepth) | ||
|
||
for depth=mindepth,maxdepth,2 do | ||
local iterations = 2 ^ (maxdepth - depth + mindepth) | ||
local check = 0 | ||
for i=1,iterations do | ||
check = check + ItemCheck(BottomUpTree(1, depth)) + | ||
ItemCheck(BottomUpTree(-1, depth)) | ||
end | ||
io.write(string.format("%d trees of depth %d check: %d\n", | ||
iterations*2, depth, check)) | ||
end | ||
|
||
io.write(string.format("long lived tree of depth %d check: %d\n", | ||
maxdepth, ItemCheck(longlivedtree))) | ||
|
||
io.write(string.format("elapsed: %.8f\n", os.clock() - start)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# The Computer Language Benchmarks Game | ||
# http://shootout.alioth.debian.org/ | ||
# | ||
# contributed by Antoine Pitrou | ||
# modified by Dominique Wahli | ||
# modified by Heinrich Acker | ||
from __future__ import print_function | ||
|
||
import time | ||
|
||
# Map "range" to an efficient range in both Python 2 and 3. | ||
try: | ||
range = xrange | ||
except NameError: | ||
pass | ||
|
||
def make_tree(item, depth): | ||
if not depth: return item, None, None | ||
item2 = item + item | ||
depth -= 1 | ||
return item, make_tree(item2 - 1, depth), make_tree(item2, depth) | ||
|
||
def check_tree(node): | ||
item, left, right = node | ||
if not left: return item | ||
return item + check_tree(left) - check_tree(right) | ||
|
||
min_depth = 4 | ||
max_depth = 12 | ||
stretch_depth = max_depth + 1 | ||
|
||
start = time.clock() | ||
print("stretch tree of depth %d check:" % stretch_depth, check_tree(make_tree(0, stretch_depth))) | ||
|
||
long_lived_tree = make_tree(0, max_depth) | ||
|
||
iterations = 2 ** max_depth | ||
for depth in range(min_depth, stretch_depth, 2): | ||
|
||
check = 0 | ||
for i in range(1, iterations + 1): | ||
check += check_tree(make_tree(i, depth)) + check_tree(make_tree(-i, depth)) | ||
|
||
print("%d trees of depth %d check:" % (iterations * 2, depth), check) | ||
iterations //= 4 | ||
|
||
print("long lived tree of depth %d check:" % max_depth, check_tree(long_lived_tree)) | ||
print("elapsed: " + str(time.clock() - start)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# The Computer Language Shootout Benchmarks | ||
# http://shootout.alioth.debian.org | ||
# | ||
# contributed by Jesse Millikan | ||
# Modified by Wesley Moxam | ||
|
||
|
||
def item_check(left, item, right) | ||
return item if left.nil? | ||
item + item_check(*left) - item_check(*right) | ||
end | ||
|
||
def bottom_up_tree(item, depth) | ||
return [nil, item, nil] unless depth > 0 | ||
item_item = 2 * item | ||
depth -= 1 | ||
[bottom_up_tree(item_item - 1, depth), item, bottom_up_tree(item_item, depth)] | ||
end | ||
|
||
max_depth = 12 | ||
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) | ||
|
||
start = Time.now | ||
puts "stretch tree of depth #{stretch_depth} 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} trees of depth #{depth} check: #{check}" | ||
end | ||
|
||
puts "long lived tree of depth #{max_depth} check: #{item_check(*long_lived_tree)}" | ||
puts "elapsed: " + (Time.now - start).to_s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Ported from the Python version. | ||
|
||
class Tree { | ||
constructor (_item, depth) { | ||
if (depth > 0) { | ||
var item2 = _item + _item | ||
depth -= 1 | ||
_left = Tree(item2 - 1, depth) | ||
_right = Tree(item2, depth) | ||
} | ||
} | ||
|
||
check { | ||
if (_left is null) { | ||
return _item | ||
} | ||
return _item + _left.check - _right.check | ||
} | ||
} | ||
|
||
var minDepth = 4 | ||
var maxDepth = 12 | ||
var stretchDepth = maxDepth + 1 | ||
|
||
var start = System.clock | ||
|
||
print("stretch tree of depth " + stretchDepth + " check: " + | ||
"" + Tree(0, stretchDepth).check) | ||
|
||
var longLivedTree = Tree(0, maxDepth) | ||
|
||
var iterations = 2 ** maxDepth | ||
|
||
var depth = minDepth | ||
while (depth < stretchDepth) { | ||
var check = 0 | ||
for i in 1...iterations { | ||
check = check + Tree(i, depth).check + Tree(-i, depth).check | ||
} | ||
|
||
print("" + (iterations * 2) + " trees of depth " + depth + " check: " + check) | ||
iterations /= 4 | ||
depth += 2 | ||
} | ||
|
||
print( "long lived tree of depth " + maxDepth + " check: " + longLivedTree.check) | ||
print("elapsed: " + (System.clock - start)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Ported from the Python version. | ||
|
||
class Tree { | ||
construct new(item, depth) { | ||
_item = item | ||
if (depth > 0) { | ||
var item2 = item + item | ||
depth = depth - 1 | ||
_left = Tree.new(item2 - 1, depth) | ||
_right = Tree.new(item2, depth) | ||
} | ||
} | ||
|
||
check { | ||
if (_left == null) { | ||
return _item | ||
} | ||
|
||
return _item + _left.check - _right.check | ||
} | ||
} | ||
|
||
var minDepth = 4 | ||
var maxDepth = 12 | ||
var stretchDepth = maxDepth + 1 | ||
|
||
var start = System.clock | ||
|
||
System.print("stretch tree of depth %(stretchDepth) check: " + | ||
"%(Tree.new(0, stretchDepth).check)") | ||
|
||
var longLivedTree = Tree.new(0, maxDepth) | ||
|
||
// iterations = 2 ** maxDepth | ||
var iterations = 1 | ||
for (d in 0...maxDepth) { | ||
iterations = iterations * 2 | ||
} | ||
|
||
var depth = minDepth | ||
while (depth < stretchDepth) { | ||
var check = 0 | ||
for (i in 1..iterations) { | ||
check = check + Tree.new(i, depth).check + Tree.new(-i, depth).check | ||
} | ||
|
||
System.print("%(iterations * 2) trees of depth %(depth) check: %(check)") | ||
iterations = iterations / 4 | ||
depth = depth + 2 | ||
} | ||
|
||
System.print( | ||
"long lived tree of depth %(maxDepth) check: %(longLivedTree.check)") | ||
System.print("elapsed: %(System.clock - start)") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
function fib(n) | ||
if n < 2 then return n end | ||
return fib(n - 2) + fib(n - 1) | ||
end | ||
|
||
local start = os.clock() | ||
for i = 1, 5 do | ||
io.write(fib(28) .. "\n") | ||
end | ||
io.write(string.format("elapsed: %.8f\n", os.clock() - start)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from __future__ import print_function | ||
|
||
import time | ||
|
||
def fib(n): | ||
if n < 2: return n | ||
return fib(n - 1) + fib(n - 2) | ||
|
||
start = time.clock() | ||
for i in range(0, 5): | ||
print(fib(28)) | ||
print("elapsed: " + str(time.clock() - start)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
def fib(n) | ||
if n < 2 then | ||
n | ||
else | ||
fib(n - 1) + fib(n - 2) | ||
end | ||
end | ||
|
||
start = Time.now | ||
for i in 0...5 | ||
puts fib(28) | ||
end | ||
puts "elapsed: " + (Time.now - start).to_s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
function fib (n) { | ||
if (n < 2) return n | ||
return fib(n - 1) + fib(n - 2) | ||
} | ||
|
||
|
||
var start = System.clock | ||
for i in 1...5 { | ||
print(fib(28)) | ||
} | ||
print("elapsed: "+(System.clock - start)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Fib { | ||
static get(n) { | ||
if (n < 2) return n | ||
return get(n - 1) + get(n - 2) | ||
} | ||
} | ||
|
||
var start = System.clock | ||
for (i in 1..5) { | ||
System.print(Fib.get(28)) | ||
} | ||
System.print("elapsed: %(System.clock - start)") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
local start = os.clock() | ||
local list = {} | ||
for i = 0, 2499999 do | ||
list[i] = i | ||
end | ||
|
||
local sum = 0 | ||
for k, i in pairs(list) do | ||
sum = sum + i | ||
end | ||
io.write(sum .. "\n") | ||
io.write(string.format("elapsed: %.8f\n", os.clock() - start)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from __future__ import print_function | ||
|
||
import time | ||
|
||
# Map "range" to an efficient range in both Python 2 and 3. | ||
try: | ||
range = xrange | ||
except NameError: | ||
pass | ||
|
||
start = time.clock() | ||
list = [] | ||
for i in range(0, 2500000): | ||
list.append(i) | ||
|
||
sum = 0 | ||
for i in list: | ||
sum += i | ||
print(sum) | ||
print("elapsed: " + str(time.clock() - start)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
start = Time.now | ||
list = [] | ||
1000000.times {|i| list << i} | ||
|
||
sum = 0 | ||
list.each {|i| sum += i} | ||
puts sum | ||
puts "elapsed: " + (Time.now - start).to_s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
var list = [] | ||
|
||
var start = System.clock | ||
for i in 0..2500000: list.add(i) | ||
|
||
var sum = 0 | ||
for i in list: sum += i | ||
|
||
print(sum) | ||
print("elapsed: "+(System.clock - start)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
var list = [] | ||
|
||
var start = System.clock | ||
for (i in 0...2500000) list.add(i) | ||
|
||
var sum = 0 | ||
for (i in list) sum = sum + i | ||
|
||
System.print(sum) | ||
System.print("elapsed: %(System.clock - start)") |
Oops, something went wrong.