user-0 提问: C代码优化--数组内任意长度字节的数据赋值

回复:
user: user-1
content: 楼主是输出Release版的exe吗？  无profiler不要谈效率！！尤其在这个云计算、虚拟机、模拟器、CUDA、多核 、多级cache、指令流水线、多种存储介质、……满天飞的时代！

user: user-0
content: 是输出的release版本的，这个有影响吗，还有就是我们的cpu只有40mhz，所以希望能尽可能优化啊

user: user-2
content: 有时候把if (..)else if (..)...和if (.. || ..)把最有可能出现的情况放到前面，如果调用频繁的话，也能省下好多时间……

user: user-0
content: 合并了for 循环，实验了下，好像效果不大啊，因为循环最多4次，实验代码如下，不知道是否还有优化的余地  #include <stdio.h> #include <stdlib.h> #include <iostream> #include <time.h> #include <windows.h> #include <WinBase.h> #include <ctime>  typedef unsigned char	uint8; typedef signed char		int8; typedef unsigned short	uint16; typedef signed short	int16; typedef unsigned int	uint32; typedef signed int		int32; typedef unsigned long	uint64; typedef signed long		int64;  void PutMessageData(uint32 DATA, uint32 CanBuffIdx, uint8 CanBuffStartBit, uint16 CanBuffLength, uint8 *Tx_Buffer, uint8 CanBuffOrder); void PutMessageDataNew(uint32 DATA, uint32 CanBuffIdx, uint8 CanBuffStartBit, uint16 CanBuffLength, uint8 *Tx_Buffer, uint8 CanBuffOrder); void PutMessageDataOld(uint32 DATA,uint32 CAN_BUFF_IDX,uint8 CAN_BUFF_STARTBIT,uint16 CAN_BUFF_LENGTH,uint8 *Tx_Buffer, uint8 CAN_BUFF_ORDER);   #define INTEL 0 #define MOTO 1 #define MIXMODE 2 #define CAN_ORDER INTEL #define MAX		32 #define MIN		1   static union /* CAN data buffer definition */ { 	uint8 data_au8[8]; /* 8-bytes data frame */ 	uint64 data_u64; /* for efficient clearing of 8-bytes buffer */ } msgData[16];  #define MAX_TIME 30*100000 uint8 length_data[MAX_TIME];  using namespace std; uint8 buff[128];  int main() { 	uint64 i,j; 	uint32 data = 0x12345678; 	uint64 start_msec,end_msec; 	uint8 signal_length, signal_start_bit; 	 	srand(time(0)); 	for (i=0; i<MAX_TIME; i++) 	{ 		length_data[i] = rand() % (MAX + 1 - MIN) + MIN; 	} 	start_msec = GetTickCount(); 	for (i=0; i<MAX_TIME; i++) 	{ 		signal_length = //length_data[i];// 		signal_start_bit =9;//= rand() % (64 - signal_length  + 1 - 1) + 1; 		PutMessageDataOld(data,0,signal_start_bit,signal_length,buff,INTEL); 	} 	end_msec = GetTickCount(); 	printf("%ld\r\n",end_msec - start_msec);  	start_msec = GetTickCount(); 	for (i=0; i<MAX_TIME; i++) 	{ 		signal_length = length_data[i];// 		signal_start_bit =9;//= rand() % (64 - signal_length  + 1 - 1) + 1; 		PutMessageData(data,0,signal_start_bit,signal_length,buff,INTEL); 	} 	end_msec = GetTickCount(); 	printf("%ld\r\n",end_msec - start_msec);   	start_msec = GetTickCount(); 	for (i=0; i<MAX_TIME; i++) 	{ 		signal_length = //length_data[i];//rand() % (MAX + 1 - MIN) + MIN; 		signal_start_bit =9;//= rand() % (64 - signal_length  + 1 - 1) + 1; 		PutMessageDataNew(data,0,signal_start_bit,signal_length,buff,INTEL); 	} 	end_msec = GetTickCount(); 	printf("%ld\r\n",end_msec - start_msec); 	system("pause"); }    void PutMessageData(uint32 DATA, uint32 CanBuffIdx, uint8 CanBuffStartBit, uint16 					CanBuffLength, uint8 *Tx_Buffer, uint8 CanBuffOrder) { 	uint8 i, j; 	uint32 send_data_32, temp_data_32 = 0xFFFFFFFF; 	uint8 stbitp1, stbitp2, send_data_8, temp_data_8; 	if((CanBuffLength > 0) && (CanBuffLength <= 32) && (CanBuffStartBit < 64)) 	{ 		/*For Bit*/ 		if(CanBuffLength == 1) 		{ 			send_data_8 = 0x01 << (CanBuffStartBit % 8); 			temp_data_8 = ~send_data_8;  			if(DATA != 0) 			{ 				//send_data_8 =((uint8) (DATA<<7))>>(7-CanBuffStartBit%8); 				send_data_8 = 1 << (CanBuffStartBit % 8); 				*( Tx_Buffer + (CanBuffStartBit >> 3) + (CanBuffIdx << 3)) |= send_data_8; 			} 			else 			{ 				*( Tx_Buffer + (CanBuffStartBit >> 3) + (CanBuffIdx << 3)) &= temp_data_8; 			} 		} 		/*For Intel */ 		else if ((CAN_ORDER == INTEL) || ((CanBuffOrder == INTEL) && (CAN_ORDER == 			MIXMODE))) 		{ 			stbitp1 = CanBuffLength + CanBuffStartBit % 8; 			i = (stbitp1 - 1) >> 3; 			if((CanBuffLength + CanBuffStartBit <= 64) && (i < 4)) 			{ 				/*clear buffer*/ 				temp_data_32 = (temp_data_32 >> (32 - CanBuffLength)) << ( 					CanBuffStartBit % 8); 				temp_data_32 = ~temp_data_32; 				for(j = 0; j <= i; j++) 				{ 					*( Tx_Buffer + (CanBuffStartBit >> 3) + j + (CanBuffIdx << 3)) &= ( 						uint8)temp_data_32; 					temp_data_32 = temp_data_32 >> 8; 				} 				/*write buffer*/ 				stbitp2 = 32 - CanBuffLength; 				send_data_32 = (((uint32)(DATA << stbitp2)) >> stbitp2) << (CanBuffStartBit % 8); 				for(j = 0; j <= i; j++) 				{ 					*( Tx_Buffer + (CanBuffStartBit >> 3) + j + (CanBuffIdx << 3)) |= ( 						uint8)send_data_32; 					send_data_32 = send_data_32 >> 8; 				} 			} 		} 		/*For Moto */ 		else if((CAN_ORDER == MOTO) || ((CanBuffOrder == MOTO) && (CAN_ORDER == 			MIXMODE))) 		{ 			stbitp1 = 7 - CanBuffStartBit % 8 + CanBuffLength; 			i = (stbitp1 - 1) >> 3; 			if(((stbitp1 + (CanBuffStartBit & 0xF8)) <= 64) && (i < 4)) 			{ 				/*clear buffer*/ 				temp_data_32 = (temp_data_32 >> (32 - CanBuffLength)) << (7 - (stbitp1 - 1) % 8); 				temp_data_32 = ~temp_data_32; 				for(j = 0; j <= i; j++) 				{ 					*( Tx_Buffer + (CanBuffStartBit >> 3) + (i - j) + (CanBuffIdx << 3)) 						&= (uint8)temp_data_32; 					temp_data_32 = temp_data_32 >> 8; 				} 				/*write buffer*/ 				stbitp2 = 32 - CanBuffLength; 				send_data_32 = (((uint32)(DATA << stbitp2)) >> stbitp2) << (7 - ((stbitp1 - 1) % 8 					)); 				for(j = 0; j <= i; j++) 				{ 					*( Tx_Buffer + (CanBuffStartBit >> 3) + (i - j) + (CanBuffIdx << 3)) 						|= (uint8)send_data_32; 					send_data_32 = send_data_32 >> 8; 				} 			} 		} 	} } /*{processes...}*/ //-6-  void PutMessageDataOld(uint32 DATA,uint32 CAN_BUFF_IDX,uint8 CAN_BUFF_STARTBIT,uint16 CAN_BUFF_LENGTH,uint8 *Tx_Buffer, uint8 CAN_BUFF_ORDER) { 	uint8 i; 	uint64 temp_data_64 ; 	uint64 send_data_64 = 0xFFFFFFFFFFFFFFFF; 	uint8 stbitp1,stbitp2;  	if(CAN_BUFF_LENGTH == 0) 	{ 		return; 	}    	temp_data_64 = (uint64)DATA;   	if ((CAN_ORDER == INTEL) || ((CAN_BUFF_ORDER ==INTEL) && (CAN_ORDER == MIXMODE))) 	{  		if(CAN_BUFF_LENGTH+CAN_BUFF_STARTBIT >64) 		{ 			return; 		} 		else 		{ 			send_data_64 = send_data_64 >> CAN_BUFF_STARTBIT ; 			send_data_64 = send_data_64 << (64-CAN_BUFF_LENGTH ) ; 			send_data_64 = send_data_64 >> (64-CAN_BUFF_LENGTH-CAN_BUFF_STARTBIT ) ; 			send_data_64 = ~send_data_64; 			msgData[CAN_BUFF_IDX].data_u64 = send_data_64;  			for(i=0;i<8;i++) 			{  				*( Tx_Buffer+i+(CAN_BUFF_IDX << 3)) &= msgData[CAN_BUFF_IDX].data_au8[i]; 			} 			temp_data_64 =  temp_data_64 << CAN_BUFF_STARTBIT ; 			msgData[CAN_BUFF_IDX].data_u64 = temp_data_64; 			for(i=0;i<8;i++) 			{  				*( Tx_Buffer+i+(CAN_BUFF_IDX << 3)) |= msgData[CAN_BUFF_IDX].data_au8[i]; 			} 		}   	} 	else if((CAN_ORDER == MOTO) || ((CAN_BUFF_ORDER ==MOTO) && (CAN_ORDER == MIXMODE))) 	{ 		stbitp1 = CAN_BUFF_STARTBIT % 8; 		stbitp2 = CAN_BUFF_STARTBIT >> 3; 		CAN_BUFF_STARTBIT = (7-stbitp2)*8+stbitp1; 		CAN_BUFF_STARTBIT = (CAN_BUFF_STARTBIT+1)-CAN_BUFF_LENGTH ;  		send_data_64 = send_data_64 >> CAN_BUFF_STARTBIT ; 		send_data_64 = send_data_64 << (64-CAN_BUFF_LENGTH ) ; 		send_data_64 = send_data_64 >> (64-CAN_BUFF_LENGTH-CAN_BUFF_STARTBIT) ; 		send_data_64 = ~send_data_64; 		msgData[CAN_BUFF_IDX].data_u64 = send_data_64;  		for(i=0;i<8;i++) 		{ 			*( Tx_Buffer+i+(CAN_BUFF_IDX << 3)) &= msgData[CAN_BUFF_IDX].data_au8[7-i]; 		}  		temp_data_64 =  temp_data_64 << CAN_BUFF_STARTBIT ; 		msgData[CAN_BUFF_IDX].data_u64 = temp_data_64;  		for(i=0;i<8;i++) 		{ 			*( Tx_Buffer+i+(CAN_BUFF_IDX << 3)) |= msgData[CAN_BUFF_IDX].data_au8[7-i]; 		} 	}  } void PutMessageDataNew(uint32 DATA, uint32 CanBuffIdx, uint8 CanBuffStartBit, uint16 					   CanBuffLength, uint8 *Tx_Buffer, uint8 CanBuffOrder) { 	uint8 i, j; 	uint32 send_data_32, temp_data_32 = 0xFFFFFFFF; 	uint8 stbitp1, stbitp2, send_data_8; 	uint8 CanbuffStartByte, CanBuffLastBits, CanBuffIdxPos, *pTxBuff = NULL; 	CanBuffLastBits = CanBuffStartBit % 8; 	CanbuffStartByte = CanBuffStartBit >> 3; 	CanBuffIdxPos = CanBuffIdx << 3; 	pTxBuff = Tx_Buffer + CanbuffStartByte + CanBuffIdxPos; 	if((CanBuffLength > 0) && (CanBuffLength <= 32) && (CanBuffStartBit < 64)) 	{ 		/*For Bit*/ 		if(CanBuffLength == 1) 		{ 			send_data_8 = 0x01 << (CanBuffLastBits); 			if(DATA != 0) 			{ 				*(pTxBuff) |= send_data_8; 			} 			else 			{ 				*(pTxBuff) &= (~send_data_8); 			} 		} 		/*For Intel */ 		else if ((CAN_ORDER == INTEL) || ((CanBuffOrder == INTEL) && (CAN_ORDER == MIXMODE))) 		{ 			stbitp1 = CanBuffLength + CanBuffLastBits; 			i = (stbitp1 - 1) >> 3; 			if((CanBuffLength + CanBuffStartBit <= 64) && (i < 4)) 			{ 				/*clear buffer*/ 				temp_data_32 = (temp_data_32 >> (32 - CanBuffLength)) << CanBuffLastBits; 				temp_data_32 = ~temp_data_32; 				stbitp2 = 32 - CanBuffLength; 				send_data_32 = (((uint32)(DATA << stbitp2)) >> stbitp2) << (CanBuffLastBits); 				for(j = 0; j <= i; j++) 				{ 					*( pTxBuff + j) &= (uint8)temp_data_32; 					temp_data_32 = temp_data_32 >> 8; 					*( pTxBuff + j) |= (uint8)send_data_32; 					send_data_32 = send_data_32 >> 8; 				} 			} 		} 		/*For Moto */ 		else if((CAN_ORDER == MOTO) || ((CanBuffOrder == MOTO) && (CAN_ORDER == 			MIXMODE))) 		{ 			stbitp1 = 7 - CanBuffLastBits + CanBuffLength; 			i = (stbitp1 - 1) >> 3; 			if(((stbitp1 + (CanBuffStartBit & 0xF8)) <= 64) && (i < 4)) 			{ 				/*clear buffer*/ 				temp_data_32 = (temp_data_32 >> (32 - CanBuffLength)) << (7 - (stbitp1 - 1) % 8); 				temp_data_32 = ~temp_data_32; 				stbitp2 = 32 - CanBuffLength; 				send_data_32 = (((uint32)(DATA << stbitp2)) >> stbitp2) << (7 - ((stbitp1 - 1) % 8)); 				for(j = 0; j <= i; j++) 				{ 					*(pTxBuff + (i - j)) &= (uint8)temp_data_32; 					temp_data_32 = temp_data_32 >> 8; 					*(pTxBuff + (i - j)) |= (uint8)send_data_32; 					send_data_32 = send_data_32 >> 8; 				} 			} 		} 	} }

user: user-3
content: //4个for循环整合成2个。 //比如 for(j=0; j<=i; j++)                 {                     *( Tx_Buffer+(CanBuffStartBit>>3) +j+ (CanBuffIdx << 3)) &= (                                 uint8)temp_data_32;                     temp_data_32 = temp_data_32>>8;                 }                 /*write buffer*/                 stbitp2 = 32 - CanBuffLength;                 send_data_32 = (((uint32)(DATA<<stbitp2))>>stbitp2)<<(CanBuffStartBit%8);                 for(j=0; j<=i; j++)                 {                     *( Tx_Buffer+(CanBuffStartBit>>3) +j+ (CanBuffIdx << 3)) |= (                                 uint8)send_data_32;                     send_data_32 = send_data_32>>8;                 } //整合到一块

user: user-0
content: 书各位大神支招啊，谢谢