Permalink
Browse files

Merge pull request #4 from nathanhammond/master

Add benchmark for the new Emscripten approach.
  • Loading branch information...
2 parents 2a854f2 + 1bde60e commit 11160ad6e04abaffba72367db5e75d4efbf0ca6c @joelgwebber committed Feb 27, 2012
Showing with 93 additions and 0 deletions.
  1. +1 −0 emscripten/box2d.js
  2. +11 −0 emscripten/index.html
  3. +81 −0 emscripten/test.js
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html>
+ <head><title>Bench2d - Emscripten, dynamic link</title></head>
+
+ <body>
+ <!-- Files from https://github.com/kripken/box2d.js 8703ed67fd6c5087e46c9dcea0b8fde52c968a5f -->
+ <script src="box2d.js"></script>
+ <script src='test.js'></script>
+ </body>
+</html>
+
View
@@ -0,0 +1,81 @@
+// js -m -n -e "load('box2d.js')" test.js
+
+function clock() {
+ return Date.now();
+}
+
+var DEBUG = 1;
+
+var WARMUP = 64;
+var FRAMES = 256;
+
+var e_count = 40;
+
+function bench() {
+ var gravity = new Box2D.b2Vec2(0.0, -10.0);
+
+ var world = new Box2D.b2World(gravity);
+ world.SetAllowSleeping(false);
+
+ var bd = new Box2D.b2BodyDef();
+ var ground = world.CreateBody(bd);
+
+ var shape0 = new Box2D.b2EdgeShape();
+ shape0.Set(new Box2D.b2Vec2(-40.0, 0.0), new Box2D.b2Vec2(40.0, 0.0));
+ ground.CreateFixture(shape0, 0.0);
+
+ var topBody;
+
+ var a = 0.5;
+ var shape = new Box2D.b2PolygonShape();
+ shape.SetAsBox(a, a);
+
+ var x = new Box2D.b2Vec2(-7.0, 0.75);
+ var y = new Box2D.b2Vec2();
+ var deltaX = new Box2D.b2Vec2(0.5625, 1);
+ var deltaY = new Box2D.b2Vec2(1.125, 0.0);
+
+ for (var i = 0; i < e_count; ++i) {
+ y.set_x(x.get_x());
+ y.set_y(x.get_y());
+
+ for (var j = i; j < e_count; ++j) {
+ var bd = new Box2D.b2BodyDef();
+ bd.set_type(Box2D.b2_dynamicBody);
+ bd.set_position(y);
+ var body = world.CreateBody(bd);
+ body.CreateFixture(shape, 5.0);
+
+ topBody = body;
+
+ y.op_add(deltaY);
+ }
+
+ x.op_add(deltaX);
+ }
+
+ for (var i = 0; i < WARMUP; ++i) {
+ world.Step(1.0/60.0, 3, 3);
+ }
+
+ var times = new Array(FRAMES);
+ for (var i = 0; i < FRAMES; ++i) {
+ var start = clock();
+ world.Step(1.0/60.0, 3, 3);
+ var end = clock();
+ times[i] = end - start;
+ if (DEBUG) print([topBody.GetPosition().get_y(), topBody.GetMass()]);
+ print(end - start);
+ }
+
+ print('');
+
+ var total = 0;
+ for (var i = 0; i < FRAMES; ++i) {
+ total += times[i];
+ }
+ print(total/FRAMES);
+}
+
+bench();
+

0 comments on commit 11160ad

Please sign in to comment.