This commit is contained in:
Alon Zakai 2013-04-10 15:45:53 -07:00
Родитель 0c1958f51d
Коммит a4a20535e7
2 изменённых файлов: 185 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,122 @@
//
// Based on joelgwebber's Box2D benchmarks,
// https://github.com/joelgwebber/bench2d/blob/master/c/Bench2d.cpp
//
// Settings =====================
// Turn this on to include the y-position of the top box in the output.
#define DEBUG 0
#define WARMUP 64
#define FRAMES 256
typedef struct {
float mean;
float stddev;
} result_t;
// ==============================
#include <cstdio>
#include <time.h>
#include <math.h>
#include "Box2D/Box2D.h"
#include "Bench2d.h"
using namespace std;
const int e_count = 40;
result_t measure(clock_t times[FRAMES]) {
float values[FRAMES];
result_t r;
float total = 0;
for (int i = 0; i < FRAMES; ++i) {
values[i] = (float)times[i] / CLOCKS_PER_SEC * 1000;
total += values[i];
}
r.mean = total / FRAMES;
float variance = 0;
for (int i = 0; i < FRAMES; ++i) {
float diff = values[i] - r.mean;
variance += diff * diff;
}
r.stddev = sqrt(variance / FRAMES);
return r;
}
result_t bench() {
// Define the gravity vector.
b2Vec2 gravity(0.0f, -10.0f);
// Construct a world object, which will hold and simulate the rigid bodies.
b2World world(gravity);
world.SetAllowSleeping(false);
{
b2BodyDef bd;
b2Body* ground = world.CreateBody(&bd);
b2EdgeShape shape;
shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
ground->CreateFixture(&shape, 0.0f);
}
b2Body* topBody;
{
float32 a = 0.5f;
b2PolygonShape shape;
shape.SetAsBox(a, a);
b2Vec2 x(-7.0f, 0.75f);
b2Vec2 y;
b2Vec2 deltaX(0.5625f, 1);
b2Vec2 deltaY(1.125f, 0.0f);
for (int32 i = 0; i < e_count; ++i) {
y = x;
for (int32 j = i; j < e_count; ++j) {
b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.position = y;
b2Body* body = world.CreateBody(&bd);
body->CreateFixture(&shape, 5.0f);
topBody = body;
y += deltaY;
}
x += deltaX;
}
}
for (int32 i = 0; i < WARMUP; ++i) {
world.Step(1.0f/60.0f, 3, 3);
}
clock_t times[FRAMES];
for (int32 i = 0; i < FRAMES; ++i) {
clock_t start = clock();
world.Step(1.0f/60.0f, 3, 3);
clock_t end = clock();
times[i] = end - start;
#if DEBUG
printf("%f :: ", topBody->GetPosition().y);
printf("%f\n", (float32)(end - start) / CLOCKS_PER_SEC * 1000);
#endif
}
return measure(times);
}

Просмотреть файл

@ -0,0 +1,63 @@
# Makefile for generating a Box2D library using Emscripten.
O = Box2D_v2.2.1/Box2D
OBJECTS = \
$(O)/Collision/b2BroadPhase.o \
$(O)/Collision/b2CollideCircle.o \
$(O)/Collision/b2CollideEdge.o \
$(O)/Collision/b2CollidePolygon.o \
$(O)/Collision/b2Collision.o \
$(O)/Collision/b2Distance.o \
$(O)/Collision/b2DynamicTree.o \
$(O)/Collision/b2TimeOfImpact.o \
$(O)/Collision/Shapes/b2ChainShape.o \
$(O)/Collision/Shapes/b2CircleShape.o \
$(O)/Collision/Shapes/b2EdgeShape.o \
$(O)/Collision/Shapes/b2PolygonShape.o \
$(O)/Common/b2BlockAllocator.o \
$(O)/Common/b2Draw.o \
$(O)/Common/b2Math.o \
$(O)/Common/b2Settings.o \
$(O)/Common/b2StackAllocator.o \
$(O)/Common/b2Timer.o \
$(O)/Dynamics/b2Body.o \
$(O)/Dynamics/b2ContactManager.o \
$(O)/Dynamics/b2Fixture.o \
$(O)/Dynamics/b2Island.o \
$(O)/Dynamics/b2World.o \
$(O)/Dynamics/b2WorldCallbacks.o \
$(O)/Dynamics/Contacts/b2ChainAndCircleContact.o \
$(O)/Dynamics/Contacts/b2ChainAndPolygonContact.o \
$(O)/Dynamics/Contacts/b2CircleContact.o \
$(O)/Dynamics/Contacts/b2Contact.o \
$(O)/Dynamics/Contacts/b2ContactSolver.o \
$(O)/Dynamics/Contacts/b2EdgeAndCircleContact.o \
$(O)/Dynamics/Contacts/b2EdgeAndPolygonContact.o \
$(O)/Dynamics/Contacts/b2PolygonAndCircleContact.o \
$(O)/Dynamics/Contacts/b2PolygonContact.o \
$(O)/Dynamics/Joints/b2DistanceJoint.o \
$(O)/Dynamics/Joints/b2FrictionJoint.o \
$(O)/Dynamics/Joints/b2GearJoint.o \
$(O)/Dynamics/Joints/b2Joint.o \
$(O)/Dynamics/Joints/b2MouseJoint.o \
$(O)/Dynamics/Joints/b2PrismaticJoint.o \
$(O)/Dynamics/Joints/b2PulleyJoint.o \
$(O)/Dynamics/Joints/b2RevoluteJoint.o \
$(O)/Dynamics/Joints/b2RopeJoint.o \
$(O)/Dynamics/Joints/b2WeldJoint.o \
$(O)/Dynamics/Joints/b2WheelJoint.o \
$(O)/Rope/b2Rope.o
all: box2d.o
OPTS = -O2
%.o: %.cpp
$(CXX) -IBox2D_v2.2.1 $< -o $@ $(OPTS)
box2d.o: $(OBJECTS)
$(CXX) -o $@ $(OBJECTS) $(OPTS)
clean:
rm box2d.o