我有一个包含记录元素的 xml 文件。我正在将记录元素内的数据转换为 PCAP 格式。代码如下所示。在 do 循环中,我只花费了 6 毫秒,但是当我在循环的边界处花费时间时,它显示我花费了 43 毫秒.. 这种差异来自哪里?另外,我正在更新循环内的内存。这是可以接受的做法还是我应该将内存归零并重用它?重用会带来任何性能提升吗?循环内的时间如下图所示
2012-05-14 07:26:48 ThreadID(19404) TRACE:开始时间:1030466589,结束时间:1030466589
2012-05-14 07:26:48 ThreadID(19404) TRACE:开始时间:1030466590,结束时间:1030466590
2012-05-14 07:26:48 ThreadID(19404) TRACE:开始时间:1030466591,结束时间:1030466591
2012-05-14 07:26:48 ThreadID(19404) TRACE:开始时间:1030466591,结束时间:1030466591
2012-05-14 07:26:48 ThreadID(19404) TRACE:开始时间:1030466592,结束时间:1030466592
2012-05-14 07:26:48 ThreadID(19404) TRACE:开始时间:1030466593,结束时间:1030466593
2012-05-14 07:26:48 ThreadID(19404) TRACE:开始时间:1030466593,结束时间:1030466593
2012-05-14 07:26:48 ThreadID(19404) TRACE:开始时间:1030466594,结束时间:1030466594
2012-05-14 07:26:48 ThreadID(19404) TRACE:开始时间:1030466595,结束时间:1030466595
但是 do 循环边界的时间是 2012-05-14 07:26:48 ThreadID(19404) TRACE:start Time:1030466584,End Time:1030466627
static struct tms st_cpu;
static struct tms et_cpu;
clock_t end_time;
clock_t start_time;
char szData[100] = {0};
clock_t st_total_time;
clock_t et_total_time;
static struct tms st_total_cpu;
static struct tms et_total_cpu;
st_total_time = times(&st_total_cpu);
do {
char *pBuffer = new char[m_nBufferLen];
memset(pBuffer,0,m_nBufferLen);
if(fgets(pBuffer,m_nBufferLen,fpcap) != NULL)
{
char *pRecord = NULL;
if((pRecord = strstr(pBuffer,"<rec ")) != NULL)
{
start_time = times(&st_cpu);
CXMLParser objXMLParser(pBuffer);
objXMLParser.ProcessRecord();
objPCAPGenerator.GeneratePCAPRecord(&objXMLParser);
end_time = times(&et_cpu);
sprintf(szData,"start Time:%ld,End Time:%ld",start_time,end_time);
CLog::PrintLog(DBG_TRACE,"%s",szData);
sprintf(szData,"Real Time: %ld,UserTime:%ld,SystemTime:%ld",
end_time-start_time,
(et_cpu.tms_utime + et_cpu.tms_cutime) - (st_cpu.tms_utime + st_cpu.tms_cutime),
(et_cpu.tms_stime + et_cpu.tms_cstime) -(st_cpu.tms_stime + st_cpu.tms_cstime)
);
CLog::PrintLog(DBG_TRACE,"%s",szData);
}
}
else
{
bEnd = true;
}
}while(bEnd != true);
et_total_time = times(&et_total_cpu);
sprintf(szData,"start Time:%ld,End Time:%ld",st_total_time,et_total_time);
CLog::PrintLog(DBG_TRACE,"%s",szData);
sprintf(szData,"Total Real Time: %ld,Total UserTime:%ld,Total SystemTime:%ld",
et_total_time-st_total_time,
(et_total_cpu.tms_utime + et_total_cpu.tms_cutime) - (st_total_cpu.tms_utime + st_total_cpu.tms_cutime),
(et_total_cpu.tms_stime + et_total_cpu.tms_cstime) -(st_total_cpu.tms_stime + st_total_cpu.tms_cstime)
);
CLog::PrintLog(DBG_TRACE,"%s",szData);