Skip to content

Instantly share code, notes, and snippets.

@kazuho
Created September 21, 2022 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kazuho/6486a92e2c449d0dab6322ef2f3c264b to your computer and use it in GitHub Desktop.
Save kazuho/6486a92e2c449d0dab6322ef2f3c264b to your computer and use it in GitHub Desktop.
#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