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<< "&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;test begin&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;" << std::endl;const int testCount &#61; 100000;const char* test_str &#61; "test";clock_t startTime, endTime;const std::size_t test_str_count &#61; strlen(test_str);const std::size_t buf_size &#61; testCount * test_str_count;int cur_size &#61; buf_size;char* cur_buf &#61; NULL;const int fun_count &#61; 4;int clock_res[] &#61; { 0, 0, 0, 0 };std::string name[] &#61; {"memcpy", "strcat", "strcpy", "sprintf", "string::operator&#43;", "std::stringstream"};char* mem_buf &#61; new char[buf_size&#43;1];startTime &#61; clock();char* cur_mem_buf &#61; mem_buf;cur_size &#61; buf_size;std::size_t mem_size &#61; test_str_count * sizeof(char);for (int i &#61; 0; i < testCount; i&#43;&#43;) {memcpy(cur_mem_buf, test_str, test_str_count);cur_mem_buf &#43;&#61; test_str_count;}mem_buf[buf_size] &#61; &#39;\0&#39;;endTime &#61; clock();std::cout << "memcpy cost:" << (double)(endTime - startTime) << std::endl;delete[] mem_buf;char* cat_buf &#61; new char[buf_size &#43; 1];startTime &#61; clock();*cat_buf &#61; &#39;\0&#39;;cur_buf &#61; cat_buf;cur_size &#61; buf_size&#43;1;for (int i &#61; 0; i < testCount; i&#43;&#43;) {strcat_s(cur_buf, cur_size, test_str);cur_buf &#43;&#61; test_str_count;cur_size -&#61; test_str_count;}endTime &#61; clock();std::cout << "strcat cost:" << (double)(endTime - startTime) << std::endl;delete[] cat_buf;char* cpy_buf &#61; new char[buf_size &#43; 1];startTime &#61; clock();cur_buf &#61; cpy_buf;cur_size &#61; buf_size&#43;1;for (int i &#61; 0; i < testCount; i&#43;&#43;) {strcpy_s(cur_buf, cur_size, test_str);cur_buf &#43;&#61; test_str_count;cur_size -&#61; test_str_count;}cpy_buf[buf_size] &#61; &#39;\0&#39;;endTime &#61; clock();std::cout << "strcpy cost:" << (double)(endTime - startTime) << std::endl;delete[] cpy_buf;char* test_buf &#61; new char[buf_size&#43;1];startTime &#61; clock();test_buf[0] &#61; &#39;\0&#39;;char* cur_p &#61; test_buf;cur_size &#61; buf_size&#43;1;for (int i &#61; 0; i < testCount; i&#43;&#43;) {int cur_c &#61; i * test_str_count;sprintf_s(cur_p, cur_size, "%s", test_str);cur_p &#43;&#61; test_str_count;cur_size -&#61; test_str_count;}endTime &#61; clock();std::cout << "sprintf cost:" << (double)(endTime - startTime) << std::endl;delete[] test_buf;startTime &#61; clock();std::string str;for (int i &#61; 0; i < testCount; i&#43;&#43;) {str &#43;&#61; test_str;}endTime &#61; clock();std::cout << "string::operator&#43;&#61;() cost:" << (double)(endTime - startTime) << std::endl;startTime &#61; clock();std::stringstream strstr;for (int i &#61; 0; i < testCount; i&#43;&#43;) {strstr << "test ";}endTime &#61; clock();std::cout << "stringstream cost:" << (double)(endTime - startTime) << std::endl;std::cout << "&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;test end&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;" << std::endl;