2008年3月7日星期五

使用静态变量提高performance

In Project Unpack, die "CPublic plc" will be called for many times, each calling will run "CPublic::CPublic()" once. It takes a lot of rumtime to intial again and again those variables in "CPublic::CPublic()".

The solusion is: define these varables as static, so once "CPublic::CPublic()" be call, these variables will be valued. Next time will not be valued again.

codes:

public.h

static CString sInstallDir;
static CString sApoWorkDir;
static CString sSicherungDir;
static CString sVersionA3000;

static int nDBError;
static int nADOError;
static int nVersionA3000;


public.cpp

CString CPublic::sInstallDir =_T("");
CString CPublic::sApoWorkDir =_T("");
CString CPublic::sSicherungDir =_T("");
CString CPublic::sVersionA3000 =_T("");
int CPublic::nDBError = NOT_ERROR;
int CPublic::nADOError = NOT_ERROR;
int CPublic::nVersionA3000 = NOT_ERROR;
//********************************************************************************************
CPublic::CPublic()
{
//AfxMessageBox("call public!");

sDBError = _T("");
sADOError = _T("");
//nDBError = NOT_ERROR; //otherweise the valud is default with 1598884628
//nADOError = NOT_ERROR;
sSicherungDBError = _T("");
nSicherungDBError = NOT_ERROR;
nA3queryErrorLevel = 0;
sDaSiLaufwerk =_T("");
sPaeDatDir =_T("");

bReclaimDirProtect = false;

///////////////////////////////////////////////////////////////
//
// * to avoid reloading of some functions in CPublic::CPublic()
// * by checking the value of these static variable
//
///////////////////////////////////////////////////////////////
if (CPublic::sInstallDir.IsEmpty())
CPublic::sInstallDir = (LPCTSTR)m_reg.GetRegInstallDir(); //A3000 install dir C:\\A3000

sRezepturDir = "C:\\Rezeptur"; //valude in CUnpackView::OnCheckRezeptur()

sSourceDB = (LPCTSTR)((CPublic::sInstallDir) + CString("\\Database\\Apollo.mdb"));

//static
//---- * open apllo.mdb to get the parameter : sichrungdir
if (CPublic::sVersionA3000.IsEmpty())
{
CPublic::nDBError = openDB(); //sourceDB! get the version info
//get version info from apollo.mdb table version
CPublic::nVersionA3000 = m_versionA3000.m_versionsnummer;
CPublic::sVersionA3000.Format("%d", CPublic::nVersionA3000);
//CString s;
//s.Format("%d",m_version.m_versionsnummer);
//AfxMessageBox(s);
closeDB();
}
//---- *

//static
//---- *
if (CPublic::sSicherungDir.IsEmpty())
{
CPublic::nADOError = ADO_openDB();
//Get sicherungDir from regedit else apollo.mdb
CPublic::sSicherungDir = m_reg.GetRegDasiPath();
CPublic::sSicherungDir = ( ( CPublic::sSicherungDir == _T("") || !existsDir(CPublic::sSicherungDir) ) ? sDaSiLaufwerk : CPublic::sSicherungDir );
}

//static
//sApoWorkDir not allowed be null or folder not exists
if (CPublic::sApoWorkDir.IsEmpty())
{
CPublic::sApoWorkDir = ( sPaeDatDir == _T("") ? _T("C:\\Apo_Work") : sPaeDatDir );
mdDirectory(CPublic::sApoWorkDir);
}
//---- *




//AfxMessageBox(sApoWorkDir);
if(!CPublic::sInstallDir.IsEmpty())
{
sReportUrl = (LPCTSTR) ((CPublic::sInstallDir) + CString("\\Sicherungsentpacken_report.txt"));
sA3query = (LPCTSTR) ((CPublic::sInstallDir) + CString("\\a3query"));
sA3info = (LPCTSTR) ((CPublic::sInstallDir) + CString("\\A3info"));
sTool = (LPCTSTR) ((CPublic::sInstallDir) + CString("\\tool"));
sA3Task = (LPCTSTR) ((CPublic::sInstallDir) + CString("\\A3Task"));
sA3000bat = (LPCTSTR) ((CPublic::sInstallDir) + CString("\\a3000.bat"));
sJournalBat = (LPCTSTR) ((CPublic::sInstallDir) + CString("\\Journal.bat"));
sJDcomInfo = (LPCTSTR) ((CPublic::sInstallDir) + CString("\\jdcom_information"));
sJDcomApo = (LPCTSTR) ((CPublic::sInstallDir) + CString("\\jdcom_apotheke"));
//sTgDB = (LPCTSTR) (InstallDir + CString("\\Database\\Apollo.mdb"));
//ATISERV = (LPCTSTR) (InstallDir + CString("\\ATIServ"));
//ALifeService = (LPCTSTR) (InstallDir + CString("\\ALifeService"));
}


sA3000BatProtocolFile = "C:\\_A3000_BAT_PROTOKOLL.txt";
sJournalBatProtocolFile = "C:\\_JOURNAL_BAT_journal_PROTOKOLL.txt";


}


in other classes
will be call as "CPublic::sInstallDir" but not "plc.sInstallDir"

没有评论: