C++ 字符串连接速度测试
测试函数
“memcpy”, “strcat”, “strcpy”, “sprintf”, “string::operator+”, “std::stringstream”
测试结果
连接10万次,使用是clock,单位应该是毫秒
======test begin=
memcpy cost:0
strcat cost:453
strcpy cost:440
sprintf cost:547
string::operator+=() cost:19
stringstream cost:34
======test end=
测试代码
测试代码里,对于memcpy, strcat, strcpy, sprintf,由于每次连接后,故意把指针指向了未端,所以实际应用中,strcat,strcpy, sprintf 会真的非常慢
std::cout <<std::endl<< "============================test begin=======================" << std::endl;
const int testCount = 100000;
const char* test_str = "test";
clock_t startTime, endTime;
const std::size_t test_str_count = strlen(test_str);
const std::size_t buf_size = testCount * test_str_count;
int cur_size = buf_size;
char* cur_buf = NULL;
const int fun_count = 4;
int clock_res[] = { 0, 0, 0, 0 };
std::string name[] = {
"memcpy", "strcat", "strcpy", "sprintf", "string::operator+", "std::stringstream"
};
// memcpy
char* mem_buf = new char[buf_size+1];
startTime = clock();
char* cur_mem_buf = mem_buf;
cur_size = buf_size;
std::size_t mem_size = test_str_count * sizeof(char);
for (int i = 0; i < testCount; i++) {
memcpy(cur_mem_buf, test_str, test_str_count);
cur_mem_buf += test_str_count;
//cur_size -= test_str_count;
}
mem_buf[buf_size] = '\0';
endTime = clock();
std::cout << "memcpy cost:" << (double)(endTime - startTime) << std::endl;
delete[] mem_buf;
// strcat
char* cat_buf = new char[buf_size + 1];
startTime = clock();
*cat_buf = '\0';
cur_buf = cat_buf;
cur_size = buf_size+1;
for (int i = 0; i < testCount; i++) {
strcat_s(cur_buf, cur_size, test_str);
cur_buf += test_str_count;
cur_size -= test_str_count;
}
endTime = clock();
std::cout << "strcat cost:" << (double)(endTime - startTime) << std::endl;
delete[] cat_buf;
// strcpy
char* cpy_buf = new char[buf_size + 1];
startTime = clock();
cur_buf = cpy_buf;
cur_size = buf_size+1;
for (int i = 0; i < testCount; i++) {
strcpy_s(cur_buf, cur_size, test_str);
cur_buf += test_str_count;
cur_size -= test_str_count;
}
cpy_buf[buf_size] = '\0';
endTime = clock();
std::cout << "strcpy cost:" << (double)(endTime - startTime) << std::endl;
delete[] cpy_buf;
// sprintf
char* test_buf = new char[buf_size+1];
startTime = clock();
test_buf[0] = '\0';
char* cur_p = test_buf;
cur_size = buf_size+1;
for (int i = 0; i < testCount; i++) {
int cur_c = i * test_str_count;
sprintf_s(cur_p, cur_size, "%s", test_str);
cur_p += test_str_count;
cur_size -= test_str_count;
}
endTime = clock();
std::cout << "sprintf cost:" << (double)(endTime - startTime) << std::endl;
delete[] test_buf;
// +
startTime = clock();
std::string str;
for (int i = 0; i < testCount; i++) {
str += test_str;
}
endTime = clock();
std::cout << "string::operator+=() cost:" << (double)(endTime - startTime) << std::endl;
// stringstream
startTime = clock();
std::stringstream strstr;
for (int i = 0; i < testCount; i++) {
strstr << "test ";
}
endTime = clock();
std::cout << "stringstream cost:" << (double)(endTime - startTime) << std::endl;
std::cout << "============================test end=======================" << std::endl;