我们在写代码的时候总是被领导告知 慎用sprintf函数 ,这个函数占用CPU时间较多,对于字符串拼接可以使用strcat函数替代(并未所有情形都可替代)。
可是艾兔还是好奇,这俩函数在耗时上到底有多大区别呢?
今天艾兔就亲身测试一下。
为了易于分析,艾兔分别用sprintf和strcat、strcpy函数分别打印300个字符循环100次。然后利用单片机定时器计算三者的耗时。
下面是验证代码和结果。
while(1)里面进行计时并打印:
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART3_UART_Init();
MX_TIM1_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
//WifiValue.WifiBreathingMode = 0xFF;
int Count = 0;
char buf[1000] = {0};
while (1)
{
Time1_AtWifiReilDataSend_Ms = 0;
for(Count = 0; Count < 100; Count++)
{
//300个字符
sprintf(buf,"%s","AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAn");
buf[0] = 0;
}
printf("sprintf = %d msn",Time1_AtWifiReilDataSend_Ms);
Time1_AtWifiReilDataSend_Ms = 0;
for(Count = 0; Count < 100; Count++)
{
//300个
strcat(buf,"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAn");
buf[0] = 0;
}
printf("strcat = %d msn",Time1_AtWifiReilDataSend_Ms);
Time1_AtWifiReilDataSend_Ms = 0;
for(Count = 0; Count < 100; Count++)
{
//300个
strcpy(buf,"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAn");
buf[0] = 0;
}
printf("strcpy = %d msn",Time1_AtWifiReilDataSend_Ms);
while(1);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
启动定时器,每1ms进入中断一次:
//定时器1中断回调处理函数
uint16_t Time1_AtWifiReilDataSend_Ms = 0;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim- >Instance == htim1.Instance)
{
Time1_AtWifiReilDataSend_Ms++;
}
}
测试结果:
**sprintf = 38 ms **
**strcat = 7 ms **
**strcpy = 7 ms **
结果表明分别 打印30000个字符 ,sprintf函数耗时是strcat和strcpy函数的5.43倍。
我们修改发送字符数进行进一步验证:
我们测试50次循环循环,看看15000个字符的耗时对比。
while (1)
{
Time1_AtWifiReilDataSend_Ms = 0;
for(Count = 0; Count < 50; Count++)
{
//300个字符
sprintf(buf,"%s","AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAn");
buf[0] = 0;
}
printf("sprintf = %d ms ",Time1_AtWifiReilDataSend_Ms);
Time1_AtWifiReilDataSend_Ms = 0;
for(Count = 0; Count < 50; Count++)
{
//300个
strcat(buf,"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAn");
buf[0] = 0;
}
printf("strcat = %d ms ",Time1_AtWifiReilDataSend_Ms);
Time1_AtWifiReilDataSend_Ms = 0;
for(Count = 0; Count < 50; Count++)
{
//300个
strcpy(buf,"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAn");
buf[0] = 0;
}
printf("strcpy = %d ms ",Time1_AtWifiReilDataSend_Ms);
while(1);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
**sprintf = 19 ms **
**strcat = 4 ms **
**strcpy = 3 ms **
结果表明分别 打印15000个字符 ,sprintf函数耗时是strcat和strcpy函数的4.75倍。
** 我们继续测试10次循环循环,看看3000个字符的耗时对比。**
while (1)
{
Time1_AtWifiReilDataSend_Ms = 0;
for(Count = 0; Count < 10; Count++)
{
//300个字符
sprintf(buf,"%s","AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAn");
buf[0] = 0;
}
printf("sprintf = %d ms ",Time1_AtWifiReilDataSend_Ms);
Time1_AtWifiReilDataSend_Ms = 0;
for(Count = 0; Count < 10; Count++)
{
//300个
strcat(buf,"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAn");
buf[0] = 0;
}
printf("strcat = %d ms ",Time1_AtWifiReilDataSend_Ms);
Time1_AtWifiReilDataSend_Ms = 0;
for(Count = 0; Count < 10; Count++)
{
//300个
strcpy(buf,"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAn");
buf[0] = 0;
}
printf("strcpy = %d ms ",Time1_AtWifiReilDataSend_Ms);
while(1);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
**sprintf = 4ms **
**strcat = 1 ms **
**strcpy = 1 ms **
结果表明分别打印3000个字符 ,sprintf函数耗时是strcat和strcpy函数的4倍。
结论:
因此在程序使用中如果只是对很少字符串进行操作,比如几十个,用哪个函数差别不大,但是如果对几百个字符、甚至上个字符的组包还是要考虑一下的。
-
单片机
+关注
关注
6032文章
44513浏览量
632715 -
定时器
+关注
关注
23文章
3237浏览量
114435 -
回调函数
+关注
关注
0文章
87浏览量
11543 -
sprintf函数
+关注
关注
0文章
3浏览量
6259 -
定时器中断
+关注
关注
0文章
49浏览量
11163
发布评论请先 登录
相关推荐
评论