Performance of common operations on int types

212 views
Skip to first unread message

Tim Schulte

unread,
Aug 31, 2015, 10:59:03 AM8/31/15
to golang-nuts
In C++ some operations (like pre-increment ++a) are more efficient on int32, than on the smaller type char.
I found a table comparing the relative performance of common arithmetic/bit manipulation operations:

http://www.tantalon.com/pete/cppopt/appendix.htm#AppendixB_RelativeCosts

Is this the same for Go or are bytes just as effective as ints?

Why I need this:
I have a data structure similar to a bitarray (e.g. https://gowalker.org/github.com/Workiva/go-datastructures/bitarray#BitArray),
where sequences of bits are stored in an array of some block type (like byte/int16/int32/int64).

Therefore the efficiency of arithmetic- and bit manipulation operations is very important.

Is there a preferable block type?

Ian Lance Taylor

unread,
Aug 31, 2015, 11:29:25 AM8/31/15
to Tim Schulte, golang-nuts
On Mon, Aug 31, 2015 at 1:28 AM, Tim Schulte <schu...@gmail.com> wrote:
>
> In C++ some operations (like pre-increment ++a) are more efficient on int32,
> than on the smaller type char.
> I found a table comparing the relative performance of common arithmetic/bit
> manipulation operations:
>
> http://www.tantalon.com/pete/cppopt/appendix.htm#AppendixB_RelativeCosts
>
> Is this the same for Go or are bytes just as effective as ints?

It's fairly complicated. One way to answer your question is to say
that it is the same for Go. But another, perhaps more useful, answer
is that these kinds of micro-benchmarks do not accurately predict the
performance of your actual code. You need to measure your actual
code, not micro-benchmarks.


> Why I need this:
> I have a data structure similar to a bitarray (e.g.
> https://gowalker.org/github.com/Workiva/go-datastructures/bitarray#BitArray),
> where sequences of bits are stored in an array of some block type (like
> byte/int16/int32/int64).
>
> Therefore the efficiency of arithmetic- and bit manipulation operations is
> very important.
>
> Is there a preferable block type?

You will most likely get the most efficient performance using the type
int. Of course, that is awkward since you don't know the size. In
that case, if you expect to run primarily on 64-bit machines, use
in64. Otherwise, use int32.

But whatever you do, if performance of this code matters, measure the
result.

Ian

Tim Schulte

unread,
Aug 31, 2015, 12:12:40 PM8/31/15
to golang-nuts, schu...@gmail.com
I will definitely profile the code.

Thanks for the quick reply,

Tim

unread,
Sep 3, 2015, 3:33:27 AM9/3/15
to golang-nuts
On Monday, August 31, 2015 at 4:59:03 PM UTC+2, Tim Schulte wrote:
In C++ some operations (like pre-increment ++a) are more efficient on int32, than on the smaller type char.
I found a table comparing the relative performance of common arithmetic/bit manipulation operations:

http://www.tantalon.com/pete/cppopt/appendix.htm#AppendixB_RelativeCosts

My recommendation is not to trust that table.

Tim Schulte

unread,
Sep 3, 2015, 5:01:56 AM9/3/15
to golang-nuts
I've dug out that table merely to point out what I meant. Here is a related stackoverflow question:

http://stackoverflow.com/questions/163254/on-32-bit-cpus-is-an-integer-type-more-efficient-than-a-short-type

Will definitely do some profiling... and not rely on that table ;)

Best,
Tim
Reply all
Reply to author
Forward
0 new messages