/** * * Daniel Lemire, February 2012, http://lemire.me/ * * Suppose that you want to copy an array over * another, except for some exceptions. * What is faster? * * */ #include #include #include #include #include #include #include using namespace std; typedef unsigned int uint; class ZTimer { public: struct timeval t1, t2; public: ZTimer() : t1(), t2() { gettimeofday(&t1,0); t2 = t1; } void reset() {gettimeofday(&t1,0); t2 = t1;} int elapsed() { return ((t2.tv_sec - t1.tv_sec) * 1000) + ((t2.tv_usec - t1. tv_usec) / 1000); } int split() { gettimeofday(&t2,0); return elapsed(); } }; /** * This copies the data from begin to end, except at some locations * given by exceptionpos where it must use the values from * exceptionval. We write to "out". */ __attribute__ ((noinline)) void patchedcopy(const uint * const __restrict__ begin, const uint * const end, const uint * __restrict__ exceptionval, const vector & exceptionpos, uint * __restrict__ out) { uint * __restrict__ tmpout = out; for(const uint * __restrict__ i = begin; i::const_iterator pos = exceptionpos.begin(); pos!= exceptionpos.end();++pos) { *(out + *pos) = *(exceptionval++); } } /** * This copies the data from begin to end, except at some locations * given by exceptionbitmap where it must use the values from * exceptionval. We write to "out". */ __attribute__ ((noinline)) void bitmapcopy(const uint * const __restrict__ begin, const uint * const end, const uint * __restrict__ exceptionval, const vector & exceptionbitmap, uint * __restrict__ out) { vector::const_iterator j = exceptionbitmap.begin(); for(const uint * __restrict__ i = begin; i generateArray(uint N) { vector ans(N); for(uint k = 0; k, vector > generateExceptLocations(uint N, uint density) { vector positions; vector bitmap(N/32); for(uint k = 0; k, vector >(positions,bitmap); } int main() { uint N = 1000000*32;//1024*1024*100; uint density = 100; vector data = generateArray(N); vector out(N); ZTimer z; vector exceptionvals = generateArray(N);// more than we need cout <<" density(%)\t time with patching \t time with bitmaps"<, vector > exceptionpos = generateExceptLocations( N, density); cout<<100.0/density<<"\t\t\t"; z.reset(); patchedcopy(& data[0], (& data[0]) + N, & exceptionvals[0], exceptionpos.first, &out[0]); cout<