[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
Suppose we have 0.1 billion pieces of data, and the data range is 0 ~ 0.1 billion, that is, m of data. However, some data is lost in this array, for example, 5 or 10 less. So, how can we retrieve the lost data? This question is not difficult, but it can help us expand our thinking and continuously improve the efficiency of algorithm operation.
The simplest way to solve this problem is to flag the data and output the data in sequence.
Copy to clipboardprint? Void get_lost_number (int data [], int length)
{
Int index;
Assert (NULL! = Data & 0! = Length );
Unsigned char * pFlag = (unsigned char *) malloc (length * sizeof (unsigned char ));
Memset (pFlag, 0, length * sizeof (unsigned char ));
For (index = 0; index <length; index ++ ){
If (0 = pFlag [data [index])
PFlag [data [index] = 1;
}
For (index = 0; index <length; index ++ ){
If (0 = pFlag [index])
Printf ("% d \ n", index );
}
Free (pFlag );
Return;
}
Void get_lost_number (int data [], int length)
{
Int index;
Assert (NULL! = Data & 0! = Length );
Unsigned char * pFlag = (unsigned char *) malloc (length * sizeof (unsigned char ));
Memset (pFlag, 0, length * sizeof (unsigned char ));
For (index = 0; index <length; index ++ ){
If (0 = pFlag [data [index])
PFlag [data [index] = 1;
}
For (index = 0; index <length; index ++ ){
If (0 = pFlag [index])
Printf ("% d \ n", index );
}
Free (pFlag );
Return;
} A Friend may also see that the above code needs to allocate the same length space as the original data. In fact, we can use bit to set the access flag, so the space we apply for can be reduced.
Copy to clipboardprint? Void get_lost_number (int data [], int length)
{
Int index;
Assert (NULL! = Data & 0! = Length );
Unsigned char * pFlag = (unsigned char *) malloc (length + 7)> 3 );
Memset (pFlag, 0, length * sizeof (unsigned char ));
For (index = 0; index <length; index ++ ){
If (0 = (pFlag [data [index]> 3] & (1 <(data [index] % 8 ))))
PFlag [data [index]> 3] | = 1 <(data [index] % 8 );
}
For (index = 0; index <length; index ++ ){
If (0 = (pFlag [data [index]> 3] & (1 <(data [index] % 8 ))))
Printf ("% d \ n", index );
}
Free (pFlag );
Return;
}
Void get_lost_number (int data [], int length)
{
Int index;
Assert (NULL! = Data & 0! = Length );
Unsigned char * pFlag = (unsigned char *) malloc (length + 7)> 3 );
Memset (pFlag, 0, length * sizeof (unsigned char ));
For (index = 0; index <length; index ++ ){
If (0 = (pFlag [data [index]> 3] & (1 <(data [index] % 8 ))))
PFlag [data [index]> 3] | = 1 <(data [index] % 8 );
}
For (index = 0; index <length; index ++ ){
If (0 = (pFlag [data [index]> 3] & (1 <(data [index] % 8 ))))
Printf ("% d \ n", index );
}
Free (pFlag );
Return;
} The code above has been reduced in space. So how can we calculate the data in parallel?
Copy to clipboardprint? Void get_lost_number (int data [], int length)
{
Int index;
RANGE range [4] = {0 };
Assert (NULL! = Data & 0! = Length );
Unsigned char * pFlag = (unsigned char *) malloc (length + 7)> 3 );
Memset (pFlag, 0, length * sizeof (unsigned char ));
Range [0]. start = 0, range [0]. end = length> 2;
Range [1]. start = length> 2, range [1]. end = length> 1;
Range [2]. start = length> 1, range [2]. end = length> 2*3;
Range [3]. start = length> 2*3, range [3]. end = length;
# Pragma omp parallel
For (index = 0; index <4; index ++ ){
_ Get_lost_number (data, range [index]. start, range [index]. end, pFlag );
}
For (index = 0; index <length; index ++ ){
If (0 = (pFlag [data [index]> 3] & (1 <(data [index] % 8 ))))
Printf ("% d \ n", index );
}
Free (pFlag );
Return;
}
Void get_lost_number (int data [], int length)
{
Int index;
RANGE range [4] = {0 };
Assert (NULL! = Data & 0! = Length );
Unsigned char * pFlag = (unsigned char *) malloc (length + 7)> 3 );
Memset (pFlag, 0, length * sizeof (unsigned char ));
Range [0]. start = 0, range [0]. end = length> 2;
Range [1]. start = length> 2, range [1]. end = length> 1;
Range [2]. start = length> 1, range [2]. end = length> 2*3;
Range [3]. start = length> 2*3, range [3]. end = length;
# Pragma omp parallel
For (index = 0; index <4; index ++ ){
_ Get_lost_number (data, range [index]. start, range [index]. end, pFlag );
}
For (index = 0; index <length; index ++ ){
If (0 = (pFlag [data [index]> 3] & (1 <(data [index] % 8 ))))
Printf ("% d \ n", index );
}
Free (pFlag );
Return;
} For multi-core parallel computing, we have added the subfunction _ get_lost, which is further supplemented.
Copy to clipboardprint? Typedef struct _ RANGE
{
Int start;
Int end;
} RANGE;
Void _ get_lost_number (int data [], int start, int end, unsigned char pFlag [])
{
Int index;
For (index = start; index <end; index ++ ){
If (0 = (pFlag [data [index]> 3] & (1 <(data [index] % 8 ))))
PFlag [data [index]> 3] | = 1 <(data [index] % 8 );
}
}
Typedef struct _ RANGE
{
Int start;
Int end;
} RANGE;
Void _ get_lost_number (int data [], int start, int end, unsigned char pFlag [])
{
Int index;
For (index = start; index <end; index ++ ){
If (0 = (pFlag [data [index]> 3] & (1 <(data [index] % 8 ))))
PFlag [data [index]> 3] | = 1 <(data [index] % 8 );
}
}
Summary:
(1) code optimization can be performed continuously, but not applicable to all scenarios.
(2) At present, the cpu has started to change from 2-> 4-> 8-core. If possible, friends should learn as much as possible about multi-core programming.