Paste: silly.c
Author: | ArnoldLayne |
Mode: | c |
Date: | Sat, 29 Aug 2009 02:39:21 |
Plain Text |
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
struct timeval TBeg, TEnd;
#define STARTTm gettimeofday(&TBeg, NULL)
#define ENDTm gettimeofday(&TEnd, NULL)
#define DeltaUSec \
((double)(TEnd.tv_sec - TBeg.tv_sec) * 1000000.0 + \
(double)(TEnd.tv_usec - TBeg.tv_usec))
struct Point
{
float x, y, z;
};
float pointNorm(struct Point* p)
{
return sqrt(p->x * p->x +
p->y * p->y +
p->z * p->z);
}
void initPoint(struct Point* p, int n)
{
p->x = sin(n);
p->y = cos(n) * 3.0;
float s = sin(n);
p->z = (s * s) / 2.0;
}
void normalizePoint(struct Point * p)
{
float norm = pointNorm(p);
p->x /= norm;
p->y /= norm;
p->z /= norm;
}
struct Point* makePoints(int len)
{
struct Point* points = (struct Point *)malloc(len * sizeof(struct Point));
for (int i = 0; i < len; i++) {
initPoint(&points[i], i);
}
return points;
}
void normalizePoints(struct Point * point, int len)
{
for (int i = 0; i < len; i++) {
normalizePoint(&point[i]);
}
}
void printPoint(struct Point * p)
{
printf("%f, %f, %f\n", p->x, p->y, p->z);
}
#define MAX(a,b) (a < b) ? (b) : (a)
struct Point* maxPoints(struct Point * p, int len)
{
struct Point * maxp = (struct Point*)malloc(sizeof(struct Point));
maxp->x = maxp->y = maxp->z = 0;
for (int i = 0; i < len; i++) {
maxp->x = MAX(maxp->x, p[i].x);
maxp->y = MAX(maxp->y, p[i].y);
maxp->z = MAX(maxp->z, p[i].z);
}
return maxp;
}
void benchmark(int len)
{
struct Point* points = makePoints(len);
normalizePoints(points, len);
printPoint(maxPoints(points, len));
}
int main()
{
for (int i = 0; i < 8; i++) {
STARTTm;
benchmark(5000000);
ENDTm;
printf("Time: %0.f\n", DeltaUSec / 1000.0);
}
return 0;
}
New Annotation