What is the most efficient way to represent small values in a struct?

前端 未结 15 838
星月不相逢
星月不相逢 2021-02-01 02:15

Often I find myself having to represent a structure that consists of very small values. For example, Foo has 4 values, a, b, c, d that, range from

15条回答
  •  后悔当初
    2021-02-01 02:59

    You've stated the common and ambiguous C/C++ tag.

    Assuming C++, make the data private and add getters/ setters. No, that will not cause a performance hit - providing the optimizer is turned on.

    You can then change the implementation to use the alternatives without any change to your calling code - and therefore more easily finesse the implementation based on the results of the bench tests.

    For the record, I'd expect the struct with bit fields as per @dbush to be most likely the fastest given your description.

    Note all this is around keeping the data in cache - you may also want to see if the design of the calling algorithm can help with that.

提交回复
热议问题