java 字符串拼接效率对比_C/C++&Java 字符串拼接效率对比,该怎么处理

展开全部

C/C++ code#include "stdafx.h"

#include

#include

#include

#include

#include

#include

#include

#include

#include

using namespace std;

const int TEST_TIMES = 90000;

const char APPEND_CONTENT[] = "cppmule";

const int PREALLOCATE_SIZE = strlen(APPEND_CONTENT) * TEST_TIMES + 1;

class Statistic {

public:

string item;

int used;

friend inline ostream & operator << (ostream & os, const Statistic &stat) {

os << "item: " << stat.item << endl

<< "used: " << stat.used << " ms." << endl << endl;

return os;

}

inline bool operator>(const Statistic& stat) {

return (used > stat.used);

}

inline bool operator

return (used < stat.used);

}

};

vector g_statistics;

#define BEGIN_TICK() \

clock_t start = clock();

#define END_AND_PRINT_TICK(info) \

clock_t used = clock() - start; \

Statistic stat; \

stat.item.assign(info); \

stat.used = used; \

g_statistics.push_back(stat); \

cout << info << " Used: " << used << " ms." << endl;

#define PRINT_SORT_TEST_TICKS() \

sort(g_statistics.begin(), g_statistics.end()); \

struct StatisticPrinter { \

StatisticPrinter() : order(0) {} \

void operator() (const Statistic& stat) { \

++order; \

cout << "sort order: " << order << endl \

<< stat; \

} \

int order; \

} printer; \

cout << "---------Statistics informations(sorting ascendent)-------" << endl << endl; \

for_each(g_statistics.begin(), g_statistics.end(), printer);\

cout << "----------------------" << endl;

void test_stdstring_append()

{

string str;

BEGIN_TICK();

for (int i=0; i

str.append(APPEND_CONTENT);

}

END_AND_PRINT_TICK("std::string append");

cout << "string length: " << str.length() << endl;

}

void test_stdstring_append_operator()

{

string str;

BEGIN_TICK();

for (int i=0; i

str += APPEND_CONTENT;

}

END_AND_PRINT_TICK("std::string += operator");

cout << "string length: " << str.length() << endl;

}

void test_stdostringstream()

{

ostringstream oss;

BEGIN_TICK();

for (int i=0; i

oss << APPEND_CONTENT;

}

END_AND_PRINT_TICK("std::ostringstream <

cout << "string length: " << oss.str().length() << endl;

}

void test_stdostringstream_preallocate()

{

ostringstream oss;

BEGIN_TICK();

for (int i=0; i

oss << APPEND_CONTENT;

}

END_AND_PRINT_TICK("std::ostringstream <

cout << "string length: " << oss.str().length() << endl;

}

void test_stdstring_append_operator_preallocate()

{

string str;

str.reserve(PREALLOCATE_SIZE);

cout << "capacity: " << str.capacity() << endl

<< "size: " << str.size() << endl;

//assert(str.capacity() == 1024*1024*512);

BEGIN_TICK();

for (int i=0; i

str += APPEND_CONTENT;

}

END_AND_PRINT_TICK("hava resize(PREALLOCATE_SIZE) std::string += operator");

cout << "string length: " << str.length() << endl;

}

void test_c_strcat_append()

{

char* pstr = (char*)malloc(PREALLOCATE_SIZE);

memset(pstr, 0, sizeof(pstr));

BEGIN_TICK();

for (int i=0; i

strcat(pstr, APPEND_CONTENT);

}

END_AND_PRINT_TICK("c string function strcat:");

cout << "string length: " << strlen(pstr) << endl;

free(pstr);

pstr = NULL;

}

void test_c_memcpy_append()

{

//Allocate memory

char* pstr = (char*)malloc(PREALLOCATE_SIZE);

if (NULL == pstr) {

cerr << "Can't allocate memory." << endl;

return;

}

memset(pstr, 0, PREALLOCATE_SIZE);

BEGIN_TICK();

int len = 0;

for (int i=0; i

memcpy(pstr + len, APPEND_CONTENT, strlen(APPEND_CONTENT));

len += strlen(APPEND_CONTENT);

}

END_AND_PRINT_TICK("C language memcpy append");

cout << "string length: " << strlen(pstr) << endl;

//Cleanup

free(pstr);

pstr = NULL;

}

void test_mfc_cstring_append()

{

CString str;

BEGIN_TICK();

for (int i=0; i

str.Append(APPEND_CONTENT);

}

END_AND_PRINT_TICK("MFC CString append");

cout << "string length: " << str.GetLength() << endl;

}

void test_mfc_cstring_append_operator()

{

CString str;

BEGIN_TICK();

for (int i=0; i

str += APPEND_CONTENT;

}

END_AND_PRINT_TICK("MFC CString operator append");

cout << "string length: " << str.GetLength() << endl;

}

int _tmain(int argc, _TCHAR* argv[])

{

#ifdef _DEBUG

cout << "DEBUG version." << endl;

#else

cout << "Release version." << endl;

#endif

cout << "TEST_TIME: " << TEST_TIMES << endl;

test_c_memcpy_append();

test_stdstring_append_operator();

test_stdstring_append();

test_stdostringstream();

test_stdstring_append_operator_preallocate();

test_mfc_cstring_append();

test_mfc_cstring_append_operator();

test_c_strcat_append();

PRINT_SORT_TEST_TICKS();

return 0;

}

本回答由网友推荐

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值