Commit 3d64e9e5 authored by Marius Wachtler's avatar Marius Wachtler

Add (modified) spectral_norm millibenchmark from the language shootout.

Implement zip2
parent ccc8fda6
# The Computer Language Benchmarks Game
# http://benchmarksgame.alioth.debian.org/
#
# Contributed by Sebastien Loisel
# Fixed by Isaac Gouy
# Sped up by Josh Goldfoot
# Dirtily sped up by Simon Descarpentries
# Used list comprehension by Vadim Zelenin
# 2to3
from math import sqrt
from sys import argv
# pyston does not yet support enumerate
def _enumerate(sequence):
n = 0
rtn = []
for elem in sequence:
rtn.append((n, elem))
n += 1
return rtn
def eval_A(i, j):
ij = i+j
return 1.0 / (ij * (ij + 1) / 2 + i + 1)
def eval_A_times_u(u):
local_eval_A = eval_A
return [ sum([ local_eval_A(i, j) * u_j
for j, u_j in _enumerate(u)
]
)
for i in range(len(u))
]
def eval_At_times_u(u):
local_eval_A = eval_A
return [ sum([ local_eval_A(j, i) * u_j
for j, u_j in _enumerate(u)
]
)
for i in range(len(u))
]
def eval_AtA_times_u(u):
return eval_At_times_u(eval_A_times_u(u))
def main():
#n = int(argv[1])
n = 1000 # when pyston gets faster we should increase this
u = [1] * n
local_eval_AtA_times_u = eval_AtA_times_u
for dummy in range(10):
v = local_eval_AtA_times_u(u)
u = local_eval_AtA_times_u(v)
vBv = vv = 0
for ue, ve in zip(u, v):
vBv += ue * ve
vv += ve * ve
print("%0.9f" % (sqrt(vBv/vv)))
main()
......@@ -310,6 +310,22 @@ Box* map2(Box* f, Box* container) {
return rtn;
}
Box* zip2(Box* container1, Box* container2) {
BoxedList* rtn = new BoxedList();
llvm::iterator_range<BoxIterator> range1 = container1->pyElements();
llvm::iterator_range<BoxIterator> range2 = container2->pyElements();
BoxIterator it1 = range1.begin();
BoxIterator it2 = range2.begin();
for (; it1 != range1.end() && it2 != range2.end(); ++it1, ++it2) {
BoxedTuple::GCVector elts{ *it1, *it2 };
listAppendInternal(rtn, new BoxedTuple(std::move(elts)));
}
return rtn;
}
extern "C" const ObjectFlavor notimplemented_flavor(&boxGCHandler, NULL);
BoxedClass* notimplemented_cls;
BoxedModule* builtins_module;
......@@ -459,6 +475,7 @@ void setupBuiltins() {
builtins_module->giveAttr("open", open_obj);
builtins_module->giveAttr("map", new BoxedFunction(boxRTFunction((void*)map2, LIST, 2, false)));
builtins_module->giveAttr("zip", new BoxedFunction(boxRTFunction((void*)zip2, LIST, 2, false)));
builtins_module->setattr("str", str_cls, NULL, NULL);
builtins_module->setattr("int", int_cls, NULL, NULL);
......
......@@ -26,3 +26,5 @@ class C(object):
return self
print sum([C(1), C(2), C(3)], C(4)).n
print zip([1, 2, 3, 0], ["one", "two", "three"])
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment