我們在寫代碼的時候總是被領(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ù)差別不大,但是如果對幾百個字符、甚至上個字符的組包還是要考慮一下的。
-
單片機(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
發(fā)布評論請先 登錄
相關(guān)推薦
評論