我测试了以下7个函数,我不明白为什么combine7比combine5好。因为它们仅在“()”的位置上有所不同。
有人可以向我解释吗?
这是我的代码:
#include "Common.h"
#define PLUS
#ifdef PLUS
#define INDENT 0
#define OP +
#else
#define INDENT 1
#define OP *
#endif
typedef int data_t;
typedef struct
{
long int len;
data_t *data;
}vec_rec, *vec_ptr;
vec_ptr new_vec(long int len)
{
vec_ptr result = (vec_ptr)malloc(sizeof(vec_rec)); //Allocate header structure
if(!result) return NULL;
result->len = len;
if(len > 0) //Allocate array
{
data_t* data = (data_t*)calloc(len, sizeof(data_t));
if(!data)
{
free((void*)result);
return NULL;
}
result->data = data;
}
else result->data = NULL;
return result;
}
int get_vec_element(vec_ptr v, long int index, data_t * dest)
{
if(index < 0 || index >= v->len) return 0;
*dest = v->data[index];
return 1;
}
long int vec_length(vec_ptr v)
{
return v->len;
}
data_t* get_vec_start(vec_ptr v)
{
return v->data;
}
void combine5(vec_ptr v, data_t* dest)
{
long int i;
long int length = vec_length(v);
long int limit = length - 1;
data_t* data = get_vec_start(v);
data_t acc = INDENT;
for(i = 0; i < limit; i += 2)
{
acc = (acc OP data[i]) OP data[i + 1];
}
for(; i < length; i++)
acc = acc OP data[i];
*dest = acc;
}
void combine7(vec_ptr v, data_t* dest)
{
long int i;
long int length = vec_length(v);
long int limit = length - 1;
data_t* data = get_vec_start(v);
data_t acc = INDENT;
for(i = 0; i < limit; i += 2)
{
acc = acc OP (data[i] OP data[i + 1]);
}
for(; i < length; i++)
acc = acc OP data[i];
*dest = acc;
}
std::mt19937 gen;
int roll_die() {
std::uniform_int_distribution<> dist(1, 6);
return dist(gen);
}
int main()
{
const size_t len = 10000000;
auto vec_pointer = new_vec(len);
std::generate(vec_pointer->data, vec_pointer->data + vec_pointer->len, roll_die);
std::cout << "Initialized datas..." << std::endl;
/*std::copy(vec_pointer->data, vec_pointer->data + vec_pointer->len,
std::ostream_iterator<int>(std::cout, "\t"));*/
data_t dest = 0;
utility::CStopwatch stopwatch5;
combine5(vec_pointer, &dest);
std::cout << "combine5 elapsed time(microseconds): " << stopwatch5.NowInMicro() << std::endl;
utility::CStopwatch stopwatch7;
combine7(vec_pointer, &dest);
std::cout << "combine7 elapsed time(microseconds): " << stopwatch7.NowInMicro() << std::endl;
}
这是我的结果:
Initialized datas...
combine5 elapsed time(microseconds): 16934
combine7 elapsed time(microseconds): 14858