测试规模:百万级别
在Debug模式下,CLogStr比sprintf大概慢1倍;
但是在Release模式下,CLogStr却快些。
log_str = "";与log_str.clear();效率差不多
// testLogString.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <Windows.h>
#include <__LSL_Cls_Define.h>
#include <__LogStr.h>
int main(int argc, char* argv[])
{
int order = 978451;
int login = 9;
float volume = 2.0;
double price = 145.1240;
char* szSymbol = "EURUSD";
char szBuf[256] = "";
DWORD tick_beg = 0, tick_end = 0;
__LSL_Cls::CLogStr log_str;
int i = 0;
tick_beg = GetTickCount();
for(i = 0; i < 1000000; ++i)
sprintf(szBuf, "#%d, %d buy %.2lf %s at price %.4lf successfully", order, login, volume, szSymbol, price);
tick_end = GetTickCount();
printf("sprintf total millseconds: %ld\r\n", tick_end - tick_beg);
tick_beg = GetTickCount();
for(i = 0; i < 1000000; ++i)
{ log_str = "";//log_str.clear();
log_str = log_str+"#"+order+", "+login+" buy "+volume+" "+szSymbol+" at price "+price+" successfully";
// printf("%s\n", log_str.c_str());
// Sleep(3000);
}
tick_end = GetTickCount();
printf("log_str total millseconds: %ld\r\n", tick_end - tick_beg);
return 0;
}
Debug模式下:
Release模式下:
![image](http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/CLogStrvsSprintf_BA92/image_thumb_1.png)