Efficient C Tips #3 – Avoiding post increment / decrement

本文介绍了在C/C++中如何通过避免使用后缀自增/自减操作来提高代码效率,尤其是在解引用指针的情况下。通过具体的代码示例展示了这种改进方法,并解释了为何这种方式可以提升代码质量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     by Nigel Jones

        原文地址: http://embeddedgurus.com/stack-overflow/2008/08/efficient-c-tips-3-avoiding-post-increment-decrement/

        It always seems counter intuitive to me,  but post increment / decrement(后缀自增/自减) operations in C / C++ often result in inefficient code,  particularly when de-referencing(解引用) pointers.  For example

for (i = 0, ptr = buffer; i < 8; i++)
{
 *ptr++ = i;
}

This code snippet contains two post increment operations.  With most compilers,  you’ll get better code quality by re-writing it like this:

for (i = 0, ptr = buffer; i < 8; ++i)
{
 *ptr = i;
 ++ptr;
}

        Why is this you ask? Well,  the best explanation I’ve come across to date is this one on the IAR website:

        Certainly taking the time to understand what’s going on is worthwhile. However,  if it makes your head hurt then just remember to avoid post increment / decrement operations.

        Incidentally,  you may find that on your particular target it makes no difference.  However,  this is purely a result of the fact that your target processor directly  supports the required addressing modes to make post increments efficient.  If you are interested in writing code that is universally efficient,  then avoid the use of post increment / decrement.

        You may also wonder just how much this saves you.  I’ve run some tests on various compilers / targets and have found that this coding style cuts the object code size down from zero to several percent.  I’ve never seen it increase the code size.  More to the point,  in loops,  using a pre-increment(前置增量) can save you a load / store operation per increment per loop iteration. These can add up to some serious time savings.

     // 下方评论也很精彩, 不转了!


#ifndef nuts_bolts_h #define nuts_bolts_h #define false 0 #define true 1 #define SOME_LARGE_VALUE 1.0E+38 // Axis array index values. Must start with 0 and be continuous. #define N_AXIS 6 // Number of axes #define X_AXIS 0 // Axis indexing value. #define Y_AXIS 1 #define Z_AXIS 2 #define A_AXIS 3 #define B_AXIS 4 #define C_AXIS 5 // CoreXY motor assignments. DO NOT ALTER. // NOTE: If the A and B motor axis bindings are changed, this effects the CoreXY equations. #ifdef COREXY #define A_MOTOR X_AXIS // Must be X_AXIS #define B_MOTOR Y_AXIS // Must be Y_AXIS #endif #define F_CPU SystemCoreClock // Conversions #define MM_PER_INCH (25.40f) #define INCH_PER_MM (0.0393701f) #define TICKS_PER_MICROSECOND (F_CPU/1000000) #define DELAY_MODE_DWELL 0 #define DELAY_MODE_SYS_SUSPEND 1 // Useful macros #define clear_vector(a) memset(a, 0, sizeof(a)) #define clear_vector_float(a) memset(a, 0.0, sizeof(float)*N_AXIS) // #define clear_vector_long(a) memset(a, 0.0, sizeof(long)*N_AXIS) #define max(a,b) (((a) > (b)) ? (a) : (b)) #define min(a,b) (((a) < (b)) ? (a) : (b)) #define isequal_position_vector(a,b) !(memcmp(a, b, sizeof(float)*N_AXIS)) // Bit field and masking macros #define bit(n) (1 << n) #define bit_true(x,mask) (x) |= (mask) #define bit_false(x,mask) (x) &= ~(mask) #define bit_istrue(x,mask) ((x & mask) != 0) #define bit_isfalse(x,mask) ((x & mask) == 0) // Read a floating point value from a string. Line points to the input buffer, char_counter // is the indexer pointing to the current character of the line, while float_ptr is // a pointer to the result variable. Returns true when it succeeds uint8_t read_float(char *line, uint8_t *char_counter, float *float_ptr); // Non-blocking delay function used for general operation and suspend features. void delay_sec(float seconds, uint8_t mode); // Delays variable-defined milliseconds. Compiler compatibility fix for _delay_ms(). void delay_ms(uint16_t ms); // Delays variable-defined microseconds. Compiler compatibility fix for _delay_us(). void delay_us(uint32_t us); // Computes hypotenuse, avoiding avr-gcc&#39;s bloated version and the extra error checking. float hypot_f(float x, float y); float convert_delta_vector_to_unit_vector(float *vector); float limit_value_by_axis_maximum(float *max_value, float *unit_vec); #endif
最新发布
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值