有长度为 N 的位集(大约为 500-700)。我需要计算每个仅包含 1 的子集的数量
例子
N = 32
设置 = 0* 11 *0* 111 *00* 1 *0* 1 *00* 1111 *0* 11 *00* 111 *000* 1 *0* 1 *
输出 = { [1] = 4, [2] = 2, [3] = 2, [4] = 1, [5] = 0, ... [32] = 0 }
void get_count(int tab[], int len) {
int *out = calloc(1, sizeof(*out) * INT_BIT * len);
int i, j, k;
int cur;
int count = 0;
for(i = 0; i < len; i++) {
cur = tab[i];
for(j = 0; j < INT_BIT; j++) {
count += (cur & 1);
if(!(cur & 1)) {
out[count]++;
count = 0;
}
cur >>= 1;
}
}
for(i = 0; i < INT_BIT * len; i++) {
printf("%d ", out[i]);
}
printf("\n");
free(out);
}
这个简单的操作将执行大约数十亿次。迭代每一位太慢了。如何优化这个算法?