C++ 字符串连接速度测试

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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值