展开全部
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;
}
本回答由网友推荐
已赞过
已踩过<
你对这个回答的评价是?
评论
收起