sprintf函数耗时是strcat函数的几倍呢?

电子说

1.3w人已加入

描述

我们在写代码的时候总是被领导告知 慎用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倍。

结论:

因此在程序使用中如果只是对很少字符串进行操作,比如几十个,用哪个函数差别不大,但是如果对几百个字符、甚至上个字符的组包还是要考虑一下的。

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分