C++ vsnprintf 函数性能测试

代码:


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#ifdef Linux
#include 
#include 
#endif // Linux
#include 

using namespace std;

struct test_log
{
	char buf[1024*10];
	va_list ap;
};

test_log log_item;

void info(const char* fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	vsnprintf(log_item.buf, 1024, fmt, ap);
	va_end(ap);
}

void info2(const char* fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	strcpy(log_item.buf, fmt);
	va_end(ap);
}

void info3(const char* fmt, ...)
{
}

void info4(const char* fmt, ...)
{
	strcpy(log_item.buf, fmt);
}

long long get_time()
{
#ifdef Linux

	struct timeval time_;
	gettimeofday(&time_, NULL);
	return time_.tv_sec * 1000 + time_.tv_usec / 1000;
#else
	return clock();
#endif // Linux
}

int main(void)
{
	int num = 10000000;
	long long begin = get_time();
	for (int i = 0; i < num; ++i)
	{
		info("[%d][%s]==========================", i, "weiyuanzu");
	}
	cout << "vsnprintf diff: " << get_time() - begin << endl;

	begin = get_time();
	for (int i = 0; i < num; ++i)
	{
		info2("[%d][%d]]==========================", i, i);
	}
	cout << "strcpy diff: " << get_time() - begin << endl;

	begin = get_time();
	for (int i = 0; i < num; ++i)
	{
		info3("[%d][%d]]==========================", i, i);
	}
	cout << "kong diff: " << get_time() - begin << endl;

	begin = get_time();
	for (int i = 0; i < num; ++i)
	{
		info4("[%d][%d]]==========================", i, i);
	}
	cout << "just strcpy diff: " << get_time() - begin << endl;
	return 0;
}





window  debug 版本输出:

vsnprintf diff: 14644
strcpy diff: 172
kong diff: 170
just strcpy diff: 233

window release 版本输出:

vsnprintf diff: 2058
strcpy diff: 179
kong diff: 0
just strcpy diff: 180

你可能感兴趣的:(c++,开发语言)