Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add benchmark for the new Emscripten approach. #4

Merged
merged 1 commit into from

2 participants

@nathanhammond

Alon has released a version of box2d with JS bindings that I feel is worth adding to the collection.

@nathanhammond nathanhammond commented on the diff
emscripten/test.js
((52 lines not shown))
+ }
+
+ 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()]);

This line increases run time by ~10% in Chrome.

@joelgwebber Owner

The existence of this line, or only with DEBUG enabled? If it's the very existence, that would be somewhat surprising, but I think it would be perfectly fair to remove it. But DEBUG should be off by default -- this is just a quick way to confirm that it's producing sane values without having to hook it up to a renderer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@joelgwebber
Owner

Thanks for doing this. I'll go ahead and merge it now so I can start getting up-to-date numbers. If there are further tweaks or modifications, please feel free to send 'em my way.

@joelgwebber joelgwebber merged commit 11160ad into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 25, 2012
  1. @nathanhammond
This page is out of date. Refresh to see the latest.
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
1  emscripten/box2d.js
1 addition, 0 deletions not shown
View
11 emscripten/index.html
@@ -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
81 emscripten/test.js
@@ -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()]);

This line increases run time by ~10% in Chrome.

@joelgwebber Owner

The existence of this line, or only with DEBUG enabled? If it's the very existence, that would be somewhat surprising, but I think it would be perfectly fair to remove it. But DEBUG should be off by default -- this is just a quick way to confirm that it's producing sane values without having to hook it up to a renderer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ print(end - start);
+ }
+
+ print('');
+
+ var total = 0;
+ for (var i = 0; i < FRAMES; ++i) {
+ total += times[i];
+ }
+ print(total/FRAMES);
+}
+
+bench();
+
Something went wrong with that request. Please try again.