diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-11-07 23:44:46 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-11-07 23:44:46 +0100 |
commit | eabcbf229e565a595251f4792aa46b6879a71a17 (patch) | |
tree | ec0eab47202be096d0c35d5ec70d20a1404ef4ec | |
parent | use posix_fadvise (diff) | |
download | get-luminosity-eabcbf229e565a595251f4792aa46b6879a71a17.tar.gz get-luminosity-eabcbf229e565a595251f4792aa46b6879a71a17.tar.bz2 get-luminosity-eabcbf229e565a595251f4792aa46b6879a71a17.tar.xz |
read is much faster than mmap
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r-- | src/framebuffer.c | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/src/framebuffer.c b/src/framebuffer.c index 05ada4a..cf1aca8 100644 --- a/src/framebuffer.c +++ b/src/framebuffer.c @@ -37,9 +37,10 @@ int main() long height; long y; auto int fd; - auto const char* restrict mem; + register char* restrict mem; register double grand_sum = 0; double inv_width; + size_t ptr, len; if (fd = open("/dev/fb0", O_RDONLY), fd == -1) return perror("get-luminosity::framebuffer: open: /dev/fb0"), close(fd), 1; @@ -47,24 +48,31 @@ int main() if (ioctl(fd, (unsigned long)FBIOGET_VSCREENINFO, &var_info)) return perror("get-luminosity::framebuffer: ioctl"), close(fd), 1; - width = var_info.xres; - height = var_info.yres; - inv_width = 1 / (double)width; + width = var_info.xres; + height = var_info.yres; + inv_width = 1 / (double)width; + len = (size_t)(width * height * 4); - posix_fadvise(fd, 0, (size_t)(width * height * 4), - POSIX_FADV_SEQUENTIAL | POSIX_FADV_NOREUSE | POSIX_FADV_WILLNEED); - mem = mmap(NULL, (size_t)(width * height * 4), PROT_READ, MAP_SHARED, fd, (off_t)0); - if (mem == MAP_FAILED) - return perror("get-luminosity::framebuffer: mmap"), close(fd), 1; + posix_fadvise(fd, 0, len, POSIX_FADV_SEQUENTIAL | POSIX_FADV_NOREUSE | POSIX_FADV_WILLNEED); + if (mem = malloc(len), mem == NULL) + return perror("get-luminosity::framebuffer: malloc"), close(fd), 1; + for (ptr = 0; ptr < len;) + { + register ssize_t got = read(fd, mem + ptr, len - ptr); + if (got <= 0) + return perror("get-luminosity::framebuffer: read"), close(fd), free(mem), 1; + ptr += (size_t)got; + } + close(fd); for (y = 0; y < height; y++) { register const int32_t* restrict linemem = (const int32_t*)(mem + y * width * 4); register const int32_t* restrict end = linemem + width; - double sum = 0; + register double sum = 0; while (linemem != end) { - int32_t value = *linemem++; + register int32_t value = *linemem++; sum += linearise(value, 0) + linearise(value, 1) + linearise(value, 2); } grand_sum += sum * inv_width; @@ -74,7 +82,7 @@ int main() fprintf(stderr, "%lf\n", (double)grand_sum); fflush(stdout); - close(fd); + free(mem); return 0; } |