码迷,mamicode.com
首页 > 数据库 > 详细

Using pointer to access array instead of index

时间:2019-01-28 13:50:15      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:class   for   bubuko   device   output   res   man   ice   com   

See example below firstly.

   uint8_t parity = 0;
   uint8_t index = 0;
   //flag
   gMUXTask.responseData[index++] = MUX_DATA_BIT_LOW;
   //dirty
   gMUXTask.responseData[index++] = gDeviceStatus.responseStatus.dirty;
   parity += gDeviceStatus.responseStatus.dirty;
   //smoke alarm
   gMUXTask.responseData[index++] = gDeviceStatus.responseStatus.smokeAlarm;
   parity += gDeviceStatus.responseStatus.smokeAlarm;
   //high temperature
   gMUXTask.responseData[index++] = gDeviceStatus.responseStatus.highTemperature;
   parity += gDeviceStatus.responseStatus.highTemperature;
   //low temperature
   gMUXTask.responseData[index++] = gDeviceStatus.responseStatus.lowTemperature;
   parity += gDeviceStatus.responseStatus.lowTemperature;
   //miss Head
   gMUXTask.responseData[index++] = gDeviceStatus.responseStatus.missHead;
   parity += gDeviceStatus.responseStatus.missHead;
   //parity
   gMUXTask.responseData[index++] = CalcParity(isOddAlgorithm(false), parity);
   //outputState
   gMUXTask.responseData[index++] = MUX_DATA_BIT_LOW;

The ASM code generated is below, it is around 120bytes.

技术分享图片

Another example here,

   uint8_t* p = gMUXTask.responseData;
   *p++ = MUX_DATA_BIT_LOW;
   //dirty
   *p++ = gDeviceStatus.responseStatus.dirty;
   parity += gDeviceStatus.responseStatus.dirty;
   //smoke alarm
   *p++ = gDeviceStatus.responseStatus.smokeAlarm;
   parity += gDeviceStatus.responseStatus.smokeAlarm;
   //high temperature
   *p++ = gDeviceStatus.responseStatus.highTemperature;
   parity += gDeviceStatus.responseStatus.highTemperature;
   //low temperature
   *p++ = gDeviceStatus.responseStatus.lowTemperature;
   parity += gDeviceStatus.responseStatus.lowTemperature;
   //miss Head
   *p++ = gDeviceStatus.responseStatus.missHead;
   parity += gDeviceStatus.responseStatus.missHead;
   //parity
   *p++ = CalcParity(isOddAlgorithm(false), parity);
   //outputState
   *p++ = MUX_DATA_BIT_LOW;

The ASM code generated, it is about 106 bytes.

技术分享图片

Using pointer to access an array which has a better performance.

Using pointer to access array instead of index

标签:class   for   bubuko   device   output   res   man   ice   com   

原文地址:https://www.cnblogs.com/zjbfvfv/p/10329446.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!