What is the most efficient way to merge byte arrays in C? -
say have 2 arrays of char, , each position either 1 or 0. 2 arrays calculated in different processes , sent master combined, each 1 writes range of array:
p1 : [0, 0, 0, 0, 1, 1, 0, 1]
p2 : [1, 0, 1, 1, 0, 0, 0, 0]
goal: [1, 0, 1, 1, 1, 1, 0, 1]
however, these large arrays. there super fast way of doing besides looping on 1 of them?
to clarify, should or'd.
assuming byte granularity enough, you'd want use memcpy
copy them output array:
memcpy(goal, p2, 4); memcpy(goal + 4, p1 + 4, 4);
you can further optimize letting p1
, p2
contain own ranges, eg:
char p1[4] = { 1, 1, 0, 1 }; char p2[4] = { 1, 0, 1, 1 }; char goal[8]; memcpy(goal, p2, 4); memcpy(goal + 4, p1, 4);
note may want bit vector packing - pack 8 bits each char. save lot of memory large arrays, although complicates access.
Comments
Post a Comment