I'm developing an application which needs to perform a massive copying data byte-by-byte from one addresses to another addresses. Now'm using for loop in multithread. Size of arrays can be from 100k elements to 2M elements. It works relatively fast, but not enough. Is there a faster way to perform this task?
std::vector<uchar*> src, dst
//Filling src and dst vectors with pointers. src.size() and dst.size() are equal.
for (int i=0; i<src.size();i )
*dst[i]=*src[i]
UPD: It's an image processing task where pixel is 8-bit grayscale. Ready-to-use solutions such as OpenCV isn't suitable because it's even slower (up to 20 times). Maybe GPU solution is possible?
CodePudding user response:
I'm developing an application which needs to perform a massive copying data byte-by-byte
That's very unlikely.
The only reason to create a copy is that the data is being modified in some way (and different pieces of code can't just share the same data in a "read only" way); and if the data is being modified in some way then it's very likely that you can merge the modification into the copying.
Maybe you're doing the same changes to all pixels, and it can be (e.g.) a "read 16 pixels from source, modify 16 pixels, write 16 pixels to destination" loop (where the work involved in modifying the pixels happens in parallel with pre-fetching the next pixels into cache, etc).
Maybe you're only modifying some pixels, and can do (e.g.) a lazy if( pointer_to_row[row] == NULL) { pointer_to_row[row] = create_copy_of_row(row); } modify_row(pointer_to_rows[row]);
to avoid copying all the rows of pixels you don't modify. Maybe you can create a shared memory mapping of the data and let the operating system's "copy on write" virtual memory management take care of the copying for you.
Maybe you can have some kind of journal of changes and leave the original data alone (where you might have an int get_pixel(int x, int y ) { int temp = check_journal(x, y); if(temp != NOT_PRESENT) return temp; else return get_original_pixel_data(x, y); }
.
Maybe you can combine multiple techniques (e.g. a small journal for each row of pixels, with a lazy "if/when journal for row becomes full, create new row from old row and journal, and reset the journal to empty").
CodePudding user response:
Read/write data to a vector using []
operator maybe slow. It only serve for specific value read/write operation. To copy a big data between two vector, you should use the native pointer
. Example:
std::vector<uchar> src, dst;
dst.resize(src.size());
memcpy(dst.data(), src.data(), src.size()*sizeof(uchar));
// Or
memcpy(&dst[0], &src[0], src.size()*sizeof(uchar));
NOTE:
std::vector<uchar*> src, dst
<-- vector of uchar*
is a typo?
If it's not your typo, then you may need to use vectorize instructment, but the performance may not improve much.