关于调试中的printf的坑

对于使用c/c++进行跨平台开发的项目,由于调试手段有限,利用printf打印中间信息就成为了调试中非常重要的手段。但是这种调试手段却让本人掉到了坑里。分享一下。

简介一下背景,做的项目为跨平台的项目,我们通常的方法是首先使用vs在windows下开发和调试,再使用gcc在linux下编译linux版本。程序对于时间比较敏感。

在调试过程中,为了跟踪程序运行,增加了许多的以下语句:

ACE_Date_Time at;
 at=ACE_Date_Time(ACE_OS::gettimeofday());
 printf("sudebug, at=%02d:%02d:%02d-%02d \n",at.hour(),at.minute(),at.second(),at.microsec());    

这样就可以在希望跟踪的地方打印出时间和信息。由于希望得到微秒时间,所以使用了ace来获取时间。

但是在调试过程中,发现了问题,执行时间无法满足要求。正常来说,执行一个循环需要大概5~6ms,但是现在飙升到了100ms。让我一度怀疑是我修改的代码导致的这个问题。

在通过与同事讨论之后,同事提到printf是需要耗时的,用ace获取时间之前也没怎么用过。

于是写了测试代码来测试这两个函数的耗时。

分别测试循环20次ACE_OS::gettimeofday()加printf,只printf,只ACE_OS::gettimeofday()的时间。

示例代码如下:

ACE_Date_Time at,at1, at2;
at1=ACE_Date_Time(ACE_OS::gettimeofday());
for(int i=0;i<20;i++)
{
	at=ACE_Date_Time(ACE_OS::gettimeofday());
	printf("debug print at %02d:%02d:%02d-%02d\n",at.hour(),at.minute(),at.second(),at.microsec());
}
at2=ACE_Date_Time(ACE_OS::gettimeofday());
printf("debug test acegettime and print 20 times start at %02d:%02d:%02d-%02d\n",at1.hour(),at1.minute(),at1.second(),at1.microsec());
printf("debug test acegettime and print 20 times end at %02d:%02d:%02d-%02d\n",at2.hour(),at2.minute(),at2.second(),at2.microsec());

at1=ACE_Date_Time(ACE_OS::gettimeofday());
for(int i=0;i<20;i++)
{
	printf("debug print at %02d:%02d:%02d-%02d-- time not right\n", at.hour(),at.minute(),at.second(),at.microsec());
}
at2=ACE_Date_Time(ACE_OS::gettimeofday());
printf("debug print 20 times start at %02d:%02d:%02d-%02d\n",at1.hour(),at1.minute(),at1.second(),at1.microsec());
printf("debug print 20 times end at %02d:%02d:%02d-%02d\n",at2.hour(),at2.minute(),at2.second(),at2.microsec());

			
at1=ACE_Date_Time(ACE_OS::gettimeofday());
for(int i=0;i<20;i++)
{
	at=ACE_Date_Time(ACE_OS::gettimeofday());
}
at2=ACE_Date_Time(ACE_OS::gettimeofday());
printf("debug acegettime 20 times start at %02d:%02d:%02d-%02d\n",at1.hour(),at1.minute(),at1.second(),at1.microsec());
printf("debug acegettime 20 times end at %02d:%02d:%02d-%02d\n",at2.hour(),at2.minute(),at2.second(),at2.microsec());

运行了四次,得到的运行时间如下:(单位微秒)

次数CE_OS::gettimeofday()加printf只printf只ACE_OS::gettimeofday()
150750253760
228303204960
339035185870
438063117130

可以看出,只运行获取时间的函数基本不耗时,而如果运行print,耗时增加,如果同时运行获取时间和打印,耗时会增加更多。

那么为了测试这个程序而对程序产生较大的影响是不可接受的。那么,如果又希望获得调试信息,又不想对运行时间产生影响怎么处理呢?于是想到了sprintf,把需要打印的信息先暂存下来,然后在时间不敏感的时间再一块打印。

为了测试这种方案的用时,写了如下测试代码

char log[1024];
sprintf(log,"");
at1=ACE_Date_Time(ACE_OS::gettimeofday());
for(int i=0;i<20;i++)
{
	at=ACE_Date_Time(ACE_OS::gettimeofday());
	sprintf(log,"%s debug print at %02d:%02d:%02d-%02d\n",log,at.hour(),at.minute(),at.second(),at.microsec());
}
at2=ACE_Date_Time(ACE_OS::gettimeofday());
printf("debug acegettime and sprint 20 times start at %02d:%02d:%02d-%02d\n",at1.hour(),at1.minute(),at1.second(),at1.microsec());
printf("debug acegettime and sprint 20 times end at %02d:%02d:%02d-%02d\n",at2.hour(),at2.minute(),at2.second(),at2.microsec());


       运行了四次,时间均为0,可忽略。

至于为什么printf会耗时这么长时间,可能涉及到系统的硬件资源调度方面,在本篇中不再深入探讨。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
static __time64_t __cdecl _make__time64_t ( struct tm *tb, int ultflag ) { __time64_t tmptm1, tmptm2, tmptm3; struct tm tbtemp; long dstbias = 0; long timezone = 0; _VALIDATE_RETURN( ( tb != NULL ), EINVAL, ( ( __time64_t )( -1 ) ) ) /* * First, make sure tm_year is reasonably close to being in range. */ if ( ((tmptm1 = tb->tm_year) < _BASE_YEAR - 1) || (tmptm1 > _MAX_YEAR64 + 1) ) goto err_mktime; /* * Adjust month value so it is in the range 0 - 11. This is because * we don't know how many days are in months 12, 13, 14, etc. */ if ( (tb->tm_mon < 0) || (tb->tm_mon > 11) ) { tmptm1 += (tb->tm_mon / 12); if ( (tb->tm_mon %= 12) < 0 ) { tb->tm_mon += 12; tmptm1--; } /* * Make sure year count is still in range. */ if ( (tmptm1 < _BASE_YEAR - 1) || (tmptm1 > _MAX_YEAR64 + 1) ) goto err_mktime; } /***** HERE: tmptm1 holds number of elapsed years *****/ /* * Calculate days elapsed minus one, in the given year, to the given * month. Check for leap year and adjust if necessary. */ tmptm2 = _days[tb->tm_mon]; if ( _IS_LEAP_YEAR(tmptm1) && (tb->tm_mon > 1) ) tmptm2++; /* * Calculate elapsed days since base date (midnight, 1/1/70, UTC) * * * 365 days for each elapsed year since 1970, plus one more day for * each elapsed leap year. no danger of overflow because of the range * check (above) on tmptm1. */ tmptm3 = (tmptm1 - _BASE_YEAR) * 365 + _ELAPSED_LEAP_YEARS(tmptm1); /* * elapsed days to current month (still no possible overflow) */ tmptm3 += tmptm2; /* * elapsed days to current date. */ tmptm1 = tmptm3 + (tmptm2 = (__time64_t)(tb->tm_mday)); /***** HERE: tmptm1 holds number of elapsed days *****/ /* * Calculate elapsed hours since base date */ tmptm2 = tmptm1 * 24; tmptm1 = tmptm2 + (tmptm3 = (__time64_t)tb->tm_hour); /***** HERE: tmptm1 holds number of elapsed hours *****/ /* * Calculate elapsed minutes since base date */ tmptm2 = tmptm1 * 60; tmptm1 = tmptm2 + (tmptm3 = (__time64_t)tb->tm_min); /***** HERE: tmptm1 holds number of elapsed minutes *****/ /* * Calculate elapsed seconds since base date */ tmptm2 = tmptm1 * 60; tmptm1 = tmptm2 + (tmptm3 = (__time64_t)tb->tm_sec); /***** HERE: tmptm1 holds number of elapsed seconds *****/ if ( ultflag ) { /* * Adjust for timezone. No need to check for overflow since * localtime() will check its arg value */ __tzset(); _ERRCHECK(_get_dstbias(&dstbias;)); _ERRCHECK(_get_timezone(&timezone;)); tmptm1 += timezone; /* * Convert this second count back into a time block structure. * If localtime returns NULL, return an error. */ if ( _localtime64_s(&tbtemp;, &tmptm1;) != 0 ) goto err_mktime; /* * Now must compensate for DST. The ANSI rules are to use the * passed-in tm_isdst flag if it is non-negative. Otherwise, * compute if DST applies. Recall that tbtemp has the time without * DST compensation, but has set tm_isdst correctly. */ if ( (tb->tm_isdst > 0) || ((tb->tm_isdst < 0) && (tbtemp.tm_isdst > 0)) ) { tmptm1 += dstbias; if ( _localtime64_s(&tbtemp;, &tmptm1;) != 0 ) goto err_mktime; } } else { if ( _gmtime64_s(&tbtemp;, &tmptm1;) != 0) goto err_mktime; } /***** HERE: tmptm1 holds number of elapsed seconds, adjusted *****/ /***** for local time if requested *****/ *tb = tbtemp; return tmptm1; err_mktime: /* * All errors come to here */ errno = EINVAL; return (__time64_t)(-1); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值