Permalink
Browse files

benchmarking

  • Loading branch information...
1 parent 774f992 commit a2f20e9ee1859fcf7583481974ff5613e7d8668a @tokuhirom committed Mar 9, 2012
Showing with 47 additions and 15 deletions.
  1. +8 −0 benchmark/loop.sh
  2. +4 −2 benchmark/loop/for-range.tra
  3. +1 −1 src/frame.h
  4. +1 −1 src/parser.yy
  5. +10 −0 t/tra/range.tra
  6. +23 −11 vm.inc
View
@@ -0,0 +1,8 @@
+#!/bin/sh
+scons --quiet
+
+./tora -V
+
+time ./tora benchmark/loop/for-range.tra
+time perl benchmark/loop/for-range.pl
+
@@ -1,6 +1,8 @@
+my $max = $ARGV.size() > 0 ? 0+$ARGV[0] : 30000000;
+say($max);
my $foo;
-for (1..30000000) {
- $foo = $_
+for (1..$max) {
+ $foo = $_;
}
say($foo);
View
@@ -65,7 +65,7 @@ class LexicalVarsFrame {
template<class Y>
Y* upcast() {
- return dynamic_cast<Y*>(&(*(this)));
+ return static_cast<Y*>(&(*(this)));
}
};
View
@@ -484,7 +484,7 @@ primary_expression(A) ::= int(B). { A = B; }
primary_expression(A) ::= DOUBLE_LITERAL(B). {
A = B;
}
-primary_expression(A) ::= int(B) DOTDOT int(C). {
+primary_expression(A) ::= primary_expression(B) DOTDOT primary_expression(C). {
A = new BinaryNode(NODE_RANGE, B, C);
}
primary_expression(A) ::= FALSE. {
View
@@ -0,0 +1,10 @@
+use Test::More *;
+
+my $n = 0+'10';
+my $r = 0;
+for (1..$n) {
+ $r += $_;
+}
+is($r, 55);
+
+done_testing;
View
34 vm.inc
@@ -49,8 +49,12 @@ OP_PUSH_VALUE {
OP_NEW_RANGE {
SharedPtr<Value> l = stack.back();
stack.pop_back();
- SharedPtr<Value> r = stack.back(); stack.pop_back();
- stack.push_back(new RangeValue(l->upcast<IntValue>(), r->upcast<IntValue>()));
+ SharedPtr<Value> r = stack.back();
+ stack.pop_back();
+
+ SharedPtr<IntValue> l2 = l->value_type == VALUE_TYPE_INT ? l->upcast<IntValue>() : new IntValue(l->to_int());
+ SharedPtr<IntValue> r2 = r->value_type == VALUE_TYPE_INT ? r->upcast<IntValue>() : new IntValue(r->to_int());
+ stack.push_back(new RangeValue(l2, r2));
}
OP_FUNCDEF {
@@ -850,8 +854,9 @@ OP_FOR_ITER {
auto fframe = frame_stack->back()->upcast<ForeachFrame>();
assert(fframe->type == FRAME_TYPE_FOREACH);
stack.resize(fframe->top);
- SharedPtr<Value> iter = fframe->iter;
- if (iter->value_type == VALUE_TYPE_ARRAY_ITERATOR) {
+ const SharedPtr<Value> & iter = fframe->iter;
+ switch (iter->value_type) {
+ case VALUE_TYPE_ARRAY_ITERATOR: {
SharedPtr<ArrayValue::iterator> aiter = iter->upcast<ArrayValue::iterator>();
if (aiter->counter < aiter->parent->size()) {
SharedPtr<Value> val = aiter->parent->at(aiter->counter);
@@ -862,16 +867,19 @@ OP_FOR_ITER {
} else {
stack.push_back(new StopIterationExceptionValue());
}
- } else if (iter->value_type == VALUE_TYPE_RANGE_ITERATOR) {
- SharedPtr<RangeValue::iterator> riter = iter->upcast<RangeValue::iterator>();
+ break;
+ }
+ case VALUE_TYPE_RANGE_ITERATOR: {
+ RangeValue::iterator* riter = static_cast<RangeValue::iterator*>(iter.get());
if (riter->counter <= riter->parent->right->int_value) {
- SharedPtr<IntValue> val = new IntValue(riter->counter);
+ stack.push_back(new IntValue(riter->counter));
riter->counter++;
- stack.push_back(val);
} else {
stack.push_back(new StopIterationExceptionValue());
}
- } else if (iter->value_type == VALUE_TYPE_HASH_ITERATOR) {
+ break;
+ }
+ case VALUE_TYPE_HASH_ITERATOR: {
SharedPtr<HashValue::iterator> riter = iter->upcast<HashValue::iterator>();
if (!riter->finished()) {
SharedPtr<Value> key = riter->getkey();
@@ -884,7 +892,9 @@ OP_FOR_ITER {
} else {
stack.push_back(new StopIterationExceptionValue());
}
- } else if (iter->value_type == VALUE_TYPE_OBJECT) {
+ break;
+ }
+ case VALUE_TYPE_OBJECT: {
ObjectValue * o = iter->upcast<ObjectValue>();
Package *pkg = this->find_package(o->package_id());
auto iter = pkg->find(this->symbol_table->get_id("__next__"));
@@ -910,7 +920,9 @@ OP_FOR_ITER {
} else {
this->die("'%s' does not have a '__next__' method.", symbol_table->id2name(o->package_id()).c_str());
}
- } else {
+ break;
+ }
+ default:
this->die("[BUG] TOS is not a iterator object: %s.", iter->type_str());
}
}

0 comments on commit a2f20e9

Please sign in to comment.