我有一个与性能相关的问题。比方说,我有某种结构,比如这个:
typedef struct
{
uint8_t FirstSofRec :1; //SOF byte
uint8_t SecSofRec :1; //SOF byte
uint8_t RecPending :1; //Pending flag
uint8_t Timeout :1; //Timeout flag
uint8_t RecCompleted :1; //Recievein completed flag
uint8_t CrcMatch :1; //CRC match flag
uint8_t DataLength :2; //Data length field (1 - 8)
}Reciever_flags_t;
typedef struct
{
Reciever_flags_t flags;
uint8_t SofFrame[2];
uint8_t MsgBuffer[MAX_REC_BUFF_SIZE];
uint8_t CRC;
}Reciever_struct_t;
将一个结构的内容复制到另一个结构的最快(在性能意义上,编写嵌入式代码)方法是什么?
我有以下选择:
直接指针使用:
Reciever_struct_t BASE;
Reciever_struct_t COPY;
Reciever_struct_t *PtToBase = &BASE;
Reciever_struct_t *PtToCopy = ©
*PtToCopy = *PtToBase
或者使用 uint8 指针并逐字节复制它(假设结构中没有挂起,并且我们知道它的大小)
Reciever_struct_t BASE;
Reciever_struct_t COPY;
uint8_t *CpyPtrBase = (uint8_t *)&BASE;
uint8_t *CpyPtrCopy = (uint8_t *)©
while(SizeIsNotZero--)
{
*CpyPtrCopy++ = *CpyPtrBase++
}
这个问题的主题不是关于 malloc 和 ect 之类的细节,而是关于想法。感谢您的建议,最好的问候!