Here is a program to compare the two methods proposed so far
import ( "fmt" "github.com/bradfitz/iter" ) func p(i int) { fmt.Println(i) } func plain() { for i := 0; i < 10; i++ { p(i) } } func with_iter() { for i := range iter.N(10) { p(i) } } func main() { plain() with_iter() }
Compile to create a disassembly
go build -gcflags -S iter.go
Here is a simple one (I removed the exception from the instructions)
Customization
0035 (/home/ncw/Go/iter.go:14) MOVQ $0,AX 0036 (/home/ncw/Go/iter.go:14) JMP ,38
hinges
0037 (/home/ncw/Go/iter.go:14) INCQ ,AX 0038 (/home/ncw/Go/iter.go:14) CMPQ AX,$10 0039 (/home/ncw/Go/iter.go:14) JGE $0,45 0040 (/home/ncw/Go/iter.go:15) MOVQ AX,i+-8(SP) 0041 (/home/ncw/Go/iter.go:15) MOVQ AX,(SP) 0042 (/home/ncw/Go/iter.go:15) CALL ,p+0(SB) 0043 (/home/ncw/Go/iter.go:15) MOVQ i+-8(SP),AX 0044 (/home/ncw/Go/iter.go:14) JMP ,37 0045 (/home/ncw/Go/iter.go:17) RET ,
And here s_name
Customization
0052 (/home/ncw/Go/iter.go:20) MOVQ $10,AX 0053 (/home/ncw/Go/iter.go:20) MOVQ $0,~r0+-24(SP) 0054 (/home/ncw/Go/iter.go:20) MOVQ $0,~r0+-16(SP) 0055 (/home/ncw/Go/iter.go:20) MOVQ $0,~r0+-8(SP) 0056 (/home/ncw/Go/iter.go:20) MOVQ $type.[]struct {}+0(SB),(SP) 0057 (/home/ncw/Go/iter.go:20) MOVQ AX,8(SP) 0058 (/home/ncw/Go/iter.go:20) MOVQ AX,16(SP) 0059 (/home/ncw/Go/iter.go:20) PCDATA $0,$48 0060 (/home/ncw/Go/iter.go:20) CALL ,runtime.makeslice+0(SB) 0061 (/home/ncw/Go/iter.go:20) PCDATA $0,$-1 0062 (/home/ncw/Go/iter.go:20) MOVQ 24(SP),DX 0063 (/home/ncw/Go/iter.go:20) MOVQ 32(SP),CX 0064 (/home/ncw/Go/iter.go:20) MOVQ 40(SP),AX 0065 (/home/ncw/Go/iter.go:20) MOVQ DX,~r0+-24(SP) 0066 (/home/ncw/Go/iter.go:20) MOVQ CX,~r0+-16(SP) 0067 (/home/ncw/Go/iter.go:20) MOVQ AX,~r0+-8(SP) 0068 (/home/ncw/Go/iter.go:20) MOVQ $0,AX 0069 (/home/ncw/Go/iter.go:20) LEAQ ~r0+-24(SP),BX 0070 (/home/ncw/Go/iter.go:20) MOVQ 8(BX),BP 0071 (/home/ncw/Go/iter.go:20) MOVQ BP,autotmp_0006+-32(SP) 0072 (/home/ncw/Go/iter.go:20) JMP ,74
hinges
0073 (/home/ncw/Go/iter.go:20) INCQ ,AX 0074 (/home/ncw/Go/iter.go:20) MOVQ autotmp_0006+-32(SP),BP 0075 (/home/ncw/Go/iter.go:20) CMPQ AX,BP 0076 (/home/ncw/Go/iter.go:20) JGE $0,82 0077 (/home/ncw/Go/iter.go:20) MOVQ AX,autotmp_0005+-40(SP) 0078 (/home/ncw/Go/iter.go:21) MOVQ AX,(SP) 0079 (/home/ncw/Go/iter.go:21) CALL ,p+0(SB) 0080 (/home/ncw/Go/iter.go:21) MOVQ autotmp_0005+-40(SP),AX 0081 (/home/ncw/Go/iter.go:20) JMP ,73 0082 (/home/ncw/Go/iter.go:23) RET ,
So, you can see that the iter solution is much more expensive, although it is fully included in the installation phase. There is additional instruction in the loop phase in the loop, but this is not so bad.
I would use a simple loop.