Skip to content

Instantly share code, notes, and snippets.

@OrangeTide
Created November 21, 2011 22:30
Show Gist options
  • Save OrangeTide/1384180 to your computer and use it in GitHub Desktop.
Save OrangeTide/1384180 to your computer and use it in GitHub Desktop.
benchmark of a simple 2D pixel pattern
/* bench.c : XOR pattern without any display/output to get a baseline benchmark */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#define BGRX8888(r, g, b) \
((uint32_t)((unsigned char)(b)) << 24 | \
(uint32_t)((unsigned char)(g)) << 16 | \
(uint32_t)((unsigned char)(r)) << 8)
#define XRGB8888(r, g, b) \
((uint32_t)((unsigned char)(r)) << 16 | \
(uint32_t)((unsigned char)(g)) << 8 | (unsigned char)(b))
int main()
{
unsigned i;
uint32_t *buf;
unsigned width = 640, height = 480;
unsigned x, y;
unsigned count = 10000;
size_t rowbytes = width * sizeof(*buf);
size_t buf_len = height * rowbytes;
struct timeval start_tv, end_tv;
double dur;
buf = malloc(buf_len);
gettimeofday(&start_tv, NULL);
for (i = 0; i < count; i++) {
#if 0 /* memset - should be about 25x faster */
memset(buf, 0, buf_len);
#else
uint32_t *p = buf;
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++)
p[x] = BGRX8888(((x ^ y) + i) / 7,
(x ^ y) + i, ((x ^ y) + i) / 3);
p = (void*)((char*)p + rowbytes);
}
#endif
}
gettimeofday(&end_tv, NULL);
if (end_tv.tv_usec < start_tv.tv_usec) {
end_tv.tv_sec--;
end_tv.tv_usec += 1000000;
}
end_tv.tv_usec -= start_tv.tv_usec;
end_tv.tv_sec -= start_tv.tv_sec;
dur = end_tv.tv_usec / 1000000. + end_tv.tv_sec;
printf("total:%g\n", dur);
printf("fps:%g\n", count / dur);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment