mirror of
https://git.coom.tech/drummyfish/raycastlib.git
synced 2024-11-21 20:29:59 +01:00
Add benchmark
This commit is contained in:
parent
964d22467d
commit
5ee1c370dc
2 changed files with 42 additions and 15 deletions
|
@ -245,7 +245,6 @@ void castRaySquare(Ray localRay, Vector2D *nextCellOffset,
|
||||||
collisionPointOffset->c2 = \
|
collisionPointOffset->c2 = \
|
||||||
(((int32_t) collisionPointOffset->c1) * localRay.direction.c2) /\
|
(((int32_t) collisionPointOffset->c1) * localRay.direction.c2) /\
|
||||||
((localRay.direction.c1 == 0) ? 1 : localRay.direction.c1);\
|
((localRay.direction.c1 == 0) ? 1 : localRay.direction.c1);\
|
||||||
Unit nextC2 = localRay.start.c2 + collisionPointOffset->c2;\
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define helper2(n1,n2,c)\
|
#define helper2(n1,n2,c)\
|
||||||
|
@ -355,6 +354,7 @@ void castRayMultiHit(Ray ray, int16_t (*arrayFunc)(int16_t, int16_t),
|
||||||
currentSquare.x += no.x;
|
currentSquare.x += no.x;
|
||||||
currentSquare.y += no.y;
|
currentSquare.y += no.y;
|
||||||
|
|
||||||
|
// offset into the next cell
|
||||||
currentPos.x += co.x;
|
currentPos.x += co.x;
|
||||||
currentPos.y += co.y;
|
currentPos.y += co.y;
|
||||||
}
|
}
|
||||||
|
|
53
test.c
53
test.c
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "raycastlib.h"
|
#include "raycastlib.h"
|
||||||
#include <time.h>
|
#include <sys/time.h>
|
||||||
|
|
||||||
int16_t testArrayFunc(int16_t x, int16_t y)
|
int16_t testArrayFunc(int16_t x, int16_t y)
|
||||||
{
|
{
|
||||||
|
@ -55,6 +55,42 @@ int testSingleRay(Unit startX, Unit startY, Unit dirX, Unit dirY,
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// returns milliseconds
|
||||||
|
long measureTime(void (*func)(void))
|
||||||
|
{
|
||||||
|
long start, end;
|
||||||
|
struct timeval timecheck;
|
||||||
|
|
||||||
|
gettimeofday(&timecheck, NULL);
|
||||||
|
start = (long) timecheck.tv_sec * 1000 + (long) timecheck.tv_usec / 1000;
|
||||||
|
|
||||||
|
func();
|
||||||
|
|
||||||
|
gettimeofday(&timecheck, NULL);
|
||||||
|
end = (long) timecheck.tv_sec * 1000 + (long) timecheck.tv_usec / 1000;
|
||||||
|
|
||||||
|
return end - start;
|
||||||
|
}
|
||||||
|
|
||||||
|
void benchCastRays()
|
||||||
|
{
|
||||||
|
Ray r;
|
||||||
|
|
||||||
|
r.start.x = UNITS_PER_SQUARE + UNITS_PER_SQUARE / 2;
|
||||||
|
r.start.y = 2 * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 4;
|
||||||
|
|
||||||
|
Vector2D directions[8];
|
||||||
|
|
||||||
|
for (int i = 0; i < 8; ++i)
|
||||||
|
directions[i] = angleToDirection(UNITS_PER_SQUARE / 8 * i);
|
||||||
|
|
||||||
|
for (int i = 0; i < 1000000; ++i)
|
||||||
|
{
|
||||||
|
r.direction = directions[i % 8];
|
||||||
|
castRay(r,testArrayFunc,30);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
printf("Testing raycastlib.\n");
|
printf("Testing raycastlib.\n");
|
||||||
|
@ -100,18 +136,9 @@ int main()
|
||||||
|
|
||||||
printf("benchmark:\n");
|
printf("benchmark:\n");
|
||||||
|
|
||||||
time_t start,end;
|
long t;
|
||||||
double dif;
|
t = measureTime(benchCastRays);
|
||||||
|
printf("cast 1000000 rays: %ld ms\n",t);
|
||||||
time (&start);
|
|
||||||
|
|
||||||
for (int i = 0; i < 1000; ++i)
|
|
||||||
printf("*");
|
|
||||||
|
|
||||||
time (&end);
|
|
||||||
dif = difftime (end,start);
|
|
||||||
printf ("Your calculations took %.2lf seconds to run.\n", dif );
|
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue