RM新时代网站-首页

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

sprintf函數(shù)耗時是strcat函數(shù)的幾倍呢?

冬至子 ? 來源:一起學(xué)習(xí)軟硬件設(shè)計(jì) ? 作者:哼唧狗 ? 2023-10-26 14:28 ? 次閱讀

我們在寫代碼的時候總是被領(lǐng)導(dǎo)告知 慎用sprintf函數(shù) ,這個函數(shù)占用CPU時間較多,對于字符串拼接可以使用strcat函數(shù)替代(并未所有情形都可替代)。

可是艾兔還是好奇,這倆函數(shù)在耗時上到底有多大區(qū)別呢?

今天艾兔就親身測試一下。

為了易于分析,艾兔分別用sprintf和strcat、strcpy函數(shù)分別打印300個字符循環(huán)100次。然后利用單片機(jī)定時器計(jì)算三者的耗時。

下面是驗(yàn)證代碼和結(jié)果。

while(1)里面進(jìn)行計(jì)時并打?。?/strong>

圖片

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進(jìn)入中斷一次:

圖片

//定時器1中斷回調(diào)處理函數(shù)
uint16_t Time1_AtWifiReilDataSend_Ms = 0;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    if(htim- >Instance == htim1.Instance)
    {
    Time1_AtWifiReilDataSend_Ms++;  
    }
}

測試結(jié)果:

圖片

**sprintf = 38 ms **

**strcat = 7 ms **

**strcpy = 7 ms **

結(jié)果表明分別 打印30000個字符 ,sprintf函數(shù)耗時是strcat和strcpy函數(shù)的5.43倍。

我們修改發(fā)送字符數(shù)進(jìn)行進(jìn)一步驗(yàn)證:

我們測試50次循環(huán)循環(huán),看看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 **

結(jié)果表明分別 打印15000個字符 ,sprintf函數(shù)耗時是strcat和strcpy函數(shù)的4.75倍。

** 我們繼續(xù)測試10次循環(huán)循環(huán),看看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 **

結(jié)果表明分別打印3000個字符 ,sprintf函數(shù)耗時是strcat和strcpy函數(shù)的4倍。

結(jié)論:

因此在程序使用中如果只是對很少字符串進(jìn)行操作,比如幾十個,用哪個函數(shù)差別不大,但是如果對幾百個字符、甚至上個字符的組包還是要考慮一下的。

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • 單片機(jī)
    +關(guān)注

    關(guān)注

    6034

    文章

    44546

    瀏覽量

    634341
  • 定時器
    +關(guān)注

    關(guān)注

    23

    文章

    3246

    瀏覽量

    114680
  • 回調(diào)函數(shù)
    +關(guān)注

    關(guān)注

    0

    文章

    87

    瀏覽量

    11553
  • sprintf函數(shù)
    +關(guān)注

    關(guān)注

    0

    文章

    3

    瀏覽量

    6260
  • 定時器中斷
    +關(guān)注

    關(guān)注

    0

    文章

    49

    瀏覽量

    11167
收藏 人收藏

    評論

    相關(guān)推薦

    帶你解析字符串連接函數(shù)strcat_s函數(shù)

    strcat_s函數(shù)strcat函數(shù)一樣,主要用于字符串拼接。
    發(fā)表于 11-01 11:28 ?888次閱讀

    C語言帶你解析字符串連接函數(shù)strcat_s函數(shù)

    strcat_s函數(shù)strcat函數(shù)一樣,主要用于字符串拼接。
    發(fā)表于 11-03 10:22 ?975次閱讀

    sprintf函數(shù)和STemwin里的GUI_TOUCH_Exec()函數(shù)起沖突是什么原因?qū)е碌模?/a>

    有個新問題請教,我要是用 sprintf顯示字符,為什么%d,%s都可以,而%f一直都是0.000000? 后來發(fā)現(xiàn)是調(diào)用stemwin里的GUI_TOUCH_Exec()函數(shù)有沖突,當(dāng)不調(diào)
    發(fā)表于 05-06 08:14

    sprintf函數(shù)跑飛該怎么辦?

    在寫程序時用到了sprintf函數(shù),但不太會用,程序運(yùn)行到sprintf函數(shù)時跑飛了,不知道什么問題,望各位大神指點(diǎn)一二,感激不盡!具體如下:1.圖1中想通過
    發(fā)表于 10-16 04:36

    關(guān)于sprintf()函數(shù)的用法

    一、關(guān)于sprintf()函數(shù)的用法sprintf(),指的是字符串格式化函數(shù),把格式化的數(shù)據(jù)寫入某個字符串中。int sprintf(ch
    發(fā)表于 08-23 07:10

    sprintf與printf函數(shù)的區(qū)別

    單片機(jī)中Sprint函數(shù):說明1:使用該函數(shù)時必須包含stdio.h頭文件,否則容易卡死程序說明2:sprintf與printf函數(shù)的區(qū)別:二者功能相似,但是
    發(fā)表于 08-23 06:18

    怎樣使用Sprintf函數(shù)去代替printf函數(shù)

    為什么要去重定義Sprintf函數(shù)?怎樣使用Sprintf函數(shù)去代替printf函數(shù)
    發(fā)表于 11-30 07:32

    為什么使用printf或sprintf會出現(xiàn)程序在中斷服務(wù)函數(shù)中卡死的現(xiàn)象

    為什么使用printf或sprintf會出現(xiàn)程序在中斷服務(wù)函數(shù)中卡死的現(xiàn)象?有何解決辦法?
    發(fā)表于 12-01 07:24

    sprintf函數(shù)詳細(xì)解釋

    sprintf函數(shù)詳細(xì)解釋.
    發(fā)表于 04-16 14:18 ?59次下載

    sprintf函數(shù)原型_sprintf用法

    sprintf指的是字符串格式化命令,主要功能是把格式化的數(shù)據(jù)寫入某個字符串中。sprintf 是個變參函數(shù)。使用sprintf 對于寫入buffer的字符數(shù)是沒有限制的,這就存在了b
    發(fā)表于 11-28 09:26 ?1.5w次閱讀
    <b class='flag-5'>sprintf</b><b class='flag-5'>函數(shù)</b>原型_<b class='flag-5'>sprintf</b>用法

    sprintf函數(shù)的用法_sprintf函數(shù)的實(shí)例

    sprintf指的是字符串格式化命令,主要功能是把格式化的數(shù)據(jù)寫入某個字符串中。sprintf 是個變參函數(shù)。使用sprintf 對于寫入buffer的字符數(shù)是沒有限制的,這就存在了b
    發(fā)表于 11-28 09:51 ?11.2w次閱讀
    <b class='flag-5'>sprintf</b><b class='flag-5'>函數(shù)</b>的用法_<b class='flag-5'>sprintf</b><b class='flag-5'>函數(shù)</b>的實(shí)例

    C語言模擬實(shí)現(xiàn)strcat函數(shù)

    C語言模擬實(shí)現(xiàn)strcat函數(shù)
    的頭像 發(fā)表于 06-29 16:18 ?2432次閱讀

    C語言轉(zhuǎn)換工具函數(shù)有哪些

    如果只是單個十進(jìn)制轉(zhuǎn)字符串,使用sprintf函數(shù)就可以了。
    的頭像 發(fā)表于 11-10 11:21 ?555次閱讀

    瑞薩e2studio(11)----打印函數(shù)(printf、 sprintf)的實(shí)現(xiàn)

    本篇文章主要介紹如何使用e2studio對瑞薩單片機(jī)進(jìn)行打印函數(shù)(printf、sprintf)的實(shí)現(xiàn)。
    的頭像 發(fā)表于 11-15 11:09 ?2400次閱讀
    瑞薩e2studio(11)----打印<b class='flag-5'>函數(shù)</b>(printf、 <b class='flag-5'>sprintf</b>)的實(shí)現(xiàn)

    瑞薩e2studio----打印函數(shù)(printf、sprintf)的實(shí)現(xiàn)

    本篇文章主要介紹如何使用e2studio對瑞薩單片機(jī)進(jìn)行打印函數(shù)(printf、sprintf)的實(shí)現(xiàn)。
    的頭像 發(fā)表于 11-02 17:35 ?1710次閱讀
    瑞薩e2studio----打印<b class='flag-5'>函數(shù)</b>(printf、<b class='flag-5'>sprintf</b>)的實(shí)現(xiàn)
    RM新时代网站-首页