3 ms·
The line x += int32(i) Produces the assembler line 0x001e 00030 (.\cast_test.go:10) ADDL CX, DX In comparison the code x += i is using 0x001e 00030 (.\cas
by PaulKeeble 4y ago
The line
x += int32(i)
Produces the assembler line
0x001e 00030 (.\cast_test.go:10) ADDL CX, DX
In comparison the code
x += i
is using
0x001e 00030 (.\cast_test.go:19) ADDQ CX, DX
The results are definitely different since the ADDL is for 32 bits and ADDQ is 64 bits and that is how its achieving the cast. Not sure the intent of the benchmark (the cost of a cast) is being captured directly since we have a specialist instruction for add to do the casting. But when you think about it ADDL 0, $Val achieves a cast and its 1 instruction so its not expensive at all and there may be something better than that for explicit casts, I doubt there is worse. Its basically free, I see no performance difference between them and that fits with expectations since casting is just throwing away all the top bits.
- anonymoushn 4y agoThanks. This is what I said I expected, given that int32 and int are different widths.