twitter / vireo

Vireo is a lightweight and versatile video processing library written in C++11
MIT License
927 stars 111 forks source link

Re-factor post-increments to pre-increments and reserve where performace will increase. #25

Closed shahzadlone closed 5 years ago

shahzadlone commented 5 years ago

Change all post-increments to pre-increments where necessary to improve performance.

For example consider the following:

class Integer {

  public:

  // Sample implementation of pre-incrementing.
  Integer & operator++() {
    myInt += 1;
    return *this;
  }
  // Sample implementation of post-incrementing.
  const Integer operator++(int) {
    Integer temporary = *this;
    ++*this;
    return temporary;
  }

  private:

  int myInt;
};

The additional copy (temporary) in post-incrementing step is unnecessary.

Also optimized a vector which was not being reserved even though we knew the exact number of elements that were going into it.

CLAassistant commented 5 years ago

CLA assistant check
All committers have signed the CLA.

shahzadlone commented 5 years ago

Do you need anything from me side for the merge? @CLAassistant

canbal commented 5 years ago

The post-increment and pre-increment will have no performance difference in these cases. Refactor not necessary. See https://isocpp.org/blog/2016/08/quick-q-efficiency-of-postincrement-v.s.-preincrement-in-cpp for reference.