函数中使用栈与使用堆时函数执行效率浅对比
函數1,
void uart_net_send(struct tls_uart *uart, u32 head, u32 tail, int count)
{? ? struct tls_uart_circ_buf *recv = &uart->uart_port->recv;
? ? char *uart_net_send_data = NULL;
????uart_net_send_data? = malloc(UART_NET_SEND_DATA_SIZE);
? ? memset(?uart_net_send_data , 0,? UART_NET_SEND_DATA_SIZE);
????if (count >= UART_NET_SEND_DATA_SIZE)
? ? {
? ? ? ? buflen = UART_NET_SEND_DATA_SIZE;
? ? ? ? count = count - UART_NET_SEND_DATA_SIZE;
? ? }
????MEMCPY(uart_net_send_data, recv->buf + tail, buflen);
}
函數2,
void uart_net_send(struct tls_uart *uart, u32 head, u32 tail, int count)
{? ? struct tls_uart_circ_buf *recv = &uart->uart_port->recv;
? ? char uart_net_send_data[UART_NET_SEND_DATA_SIZE] = {0};
????if (count >= UART_NET_SEND_DATA_SIZE)
? ? {
? ? ? ? buflen = UART_NET_SEND_DATA_SIZE;
? ? ? ? count = count - UART_NET_SEND_DATA_SIZE;
? ? }
????MEMCPY(uart_net_send_data, recv->buf + tail, buflen);
}
函數3,
void uart_net_send(struct tls_uart *uart, u32 head, u32 tail, int count)
{? ? struct tls_uart_circ_buf *recv = &uart->uart_port->recv;
? ? char uart_net_send_data[UART_NET_SEND_DATA_SIZE];
????if (count >= UART_NET_SEND_DATA_SIZE)
? ? {
? ? ? ? buflen = UART_NET_SEND_DATA_SIZE;
? ? ? ? count = count - UART_NET_SEND_DATA_SIZE;
? ? }
????MEMCPY(uart_net_send_data, recv->buf + tail, buflen);
}
函數uart_net_send()是一個需要快速運行完畢的函數,它的執行時間越短,設備的轉發數據的速度就越快,執行效率就越高。
經實驗,在以上三個函數實現中,函數3最慢,函數1最快。
總結
以上是生活随笔為你收集整理的函数中使用栈与使用堆时函数执行效率浅对比的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: stm32 堆和栈(stm32 Heap
- 下一篇: Android开发笔记之:Log图文详解