Skip to content

Instantly share code, notes, and snippets.

@kazuho
Created September 21, 2022 12:51
Embed
What would you like to do?
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define WIDTH (99 * 40)
#define HEIGHT (99 * 25)
static double timespec2d(struct timespec *ts)
{
return ts->tv_sec + ts->tv_nsec / 1000000000.;
}
int main(int argc, char **argv)
{
static uint8_t areas[HEIGHT][WIDTH] = {};
int modified = 0;
for (size_t y = 0; y < HEIGHT; ++y)
for (size_t x = 0; x < WIDTH; ++x)
areas[y][x] = (rand() >> 8) & 255; // このマスクを3にすると分岐予測があたらなくなる
struct timespec start, end;
timespec_get(&start, TIME_UTC);
for (size_t y = 0; y < HEIGHT; ++y) {
for (size_t x = 0; x < WIDTH; ++x) {
uint8_t id = areas[y][x];
if ((x > 0 && id == areas[y][x - 1]) ||
(y > 0 && id == areas[y - 1][x])) {
areas[y][x] = 0;
modified = 1;
}
}
}
timespec_get(&end, TIME_UTC);
printf("modified: %d\n", modified);
printf("elapsed: %f\n", timespec2d(&end) - timespec2d(&start));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment