我們在寫代碼的時候總是被領導告知 慎用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文章
44516瀏覽量
633034 -
定時器
+關注
關注
23文章
3237瀏覽量
114471 -
回調函數
+關注
關注
0文章
87瀏覽量
11543 -
sprintf函數
+關注
關注
0文章
3瀏覽量
6259 -
定時器中斷
+關注
關注
0文章
49瀏覽量
11164
發布評論請先 登錄
相關推薦
評論