c++ - Performant Way to create checkerboard pattern -
so have image want overlay checkerboard pattern. have come far:
for ( uint_8 nrow = 0; nrow < image.width(); ++nrow) (uint_8 ncol = 0; ncol < image.height(); ++ncol) if(((nrow/20 + ncol/20) % 2) == 0) memset(&image.data[ncol + nrow], 0, 1);
produces white image unfortunately. dont think performant because memset
called every single pixel in image instead of multiple. why code not produce chckerboard pattern? how improve it?
for better performance, don't treat image 2-dimensional entity. instead, @ 1d array of continuous data, lines of image arranged 1 after other.
with approach, can write pattern in 1 go single loop, in every iteration memset() multiple adjacent pixels , increase index twice amount of pixels set:
int data_size = image.width() * image.height(); (auto = image.data; < image.data + data_size; += 20) { memset(it, 0, 20); if (((it - data) + 40) % (20 * 400) == 0) { += 40; } else if (((it - data) + 20) % (20 * 400) != 0) { += 20; } }
(replace auto
type of image.data
if you're not using c++11; suspect it's unsigned char*
.)
this quite friendly cpu cache prefetch. it's friendly compiler, can potentially vectorize and/or perform loop unrolling.
Comments
Post a Comment