読者です 読者をやめる 読者になる 読者になる

ゆとりーなの日記

日記的な事を書いて行くと思はれる

だけど今日のネタはループ

ふとしたきつかけで氣になつたので、ループ周りのコードがどんなになるのか眺めてみたのでメモしておきます。

// 安直なfor文
#include <vector>

std::vector<int> v(100);

int main() {
	for (auto it = v.begin(); it != v.end(); ++it) {
		*it = 0;
	}
}
0000000000400580 <main>:
  400580:	4c 8b 05 d9 0a 20 00 	mov    0x200ad9(%rip),%r8        # 601060 <v>
  400587:	48 8b 35 da 0a 20 00 	mov    0x200ada(%rip),%rsi        # 601068 <v+0x8>
  40058e:	49 39 f0             	cmp    %rsi,%r8
  400591:	0f 84 b0 00 00 00    	je     400647 <main+0xc7>
  400597:	49 8d 40 04          	lea    0x4(%r8),%rax
  40059b:	4c 89 c1             	mov    %r8,%rcx
  40059e:	48 89 f7             	mov    %rsi,%rdi
  4005a1:	83 e1 0f             	and    $0xf,%ecx
  4005a4:	48 29 c7             	sub    %rax,%rdi
  4005a7:	48 c1 e9 02          	shr    $0x2,%rcx
  4005ab:	4c 89 c0             	mov    %r8,%rax
  4005ae:	48 c1 ef 02          	shr    $0x2,%rdi
  4005b2:	48 f7 d9             	neg    %rcx
  4005b5:	48 83 c7 01          	add    $0x1,%rdi
  4005b9:	83 e1 03             	and    $0x3,%ecx
  4005bc:	48 39 f9             	cmp    %rdi,%rcx
  4005bf:	48 0f 47 cf          	cmova  %rdi,%rcx
  4005c3:	48 83 ff 06          	cmp    $0x6,%rdi
  4005c7:	48 0f 46 cf          	cmovbe %rdi,%rcx
  4005cb:	48 85 c9             	test   %rcx,%rcx
  4005ce:	74 20                	je     4005f0 <main+0x70>
  4005d0:	31 d2                	xor    %edx,%edx
  4005d2:	66 0f 1f 44 00 00    	nopw   0x0(%rax,%rax,1)
  4005d8:	48 83 c2 01          	add    $0x1,%rdx
  4005dc:	c7 00 00 00 00 00    	movl   $0x0,(%rax)
  4005e2:	48 83 c0 04          	add    $0x4,%rax
  4005e6:	48 39 d1             	cmp    %rdx,%rcx
  4005e9:	77 ed                	ja     4005d8 <main+0x58>
  4005eb:	48 39 cf             	cmp    %rcx,%rdi
  4005ee:	74 57                	je     400647 <main+0xc7>
  4005f0:	49 89 fa             	mov    %rdi,%r10
  4005f3:	49 29 ca             	sub    %rcx,%r10
  4005f6:	4c 89 d7             	mov    %r10,%rdi
  4005f9:	48 c1 ef 02          	shr    $0x2,%rdi
  4005fd:	4c 8d 0c bd 00 00 00 	lea    0x0(,%rdi,4),%r9
  400604:	00 
  400605:	4d 85 c9             	test   %r9,%r9
  400608:	74 2e                	je     400638 <main+0xb8>
  40060a:	66 0f ef c0          	pxor   %xmm0,%xmm0
  40060e:	49 8d 0c 88          	lea    (%r8,%rcx,4),%rcx
  400612:	31 d2                	xor    %edx,%edx
  400614:	0f 1f 40 00          	nopl   0x0(%rax)
  400618:	48 83 c2 01          	add    $0x1,%rdx
  40061c:	66 0f 7f 01          	movdqa %xmm0,(%rcx)
  400620:	48 83 c1 10          	add    $0x10,%rcx
  400624:	48 39 fa             	cmp    %rdi,%rdx
  400627:	72 ef                	jb     400618 <main+0x98>
  400629:	4d 39 ca             	cmp    %r9,%r10
  40062c:	4a 8d 04 88          	lea    (%rax,%r9,4),%rax
  400630:	74 15                	je     400647 <main+0xc7>
  400632:	66 0f 1f 44 00 00    	nopw   0x0(%rax,%rax,1)
  400638:	c7 00 00 00 00 00    	movl   $0x0,(%rax)
  40063e:	48 83 c0 04          	add    $0x4,%rax
  400642:	48 39 c6             	cmp    %rax,%rsi
  400645:	75 f1                	jne    400638 <main+0xb8>
  400647:	31 c0                	xor    %eax,%eax
  400649:	c3                   	retq   
  40064a:	66 0f 1f 44 00 00    	nopw   0x0(%rax,%rax,1)
// 安定思考のend外に出す
#include <vector>

std::vector<int> v(100);

int main() {
	const auto end = v.end();
	for (auto it = v.begin(); it != end; ++it) {
		*it = 0;
	}
}
0000000000400580 <main>:
  400580:	48 8b 35 e1 0a 20 00 	mov    0x200ae1(%rip),%rsi        # 601068 <v+0x8>
  400587:	4c 8b 05 d2 0a 20 00 	mov    0x200ad2(%rip),%r8        # 601060 <v>
  40058e:	4c 39 c6             	cmp    %r8,%rsi
  400591:	0f 84 b0 00 00 00    	je     400647 <main+0xc7>
  400597:	49 8d 40 04          	lea    0x4(%r8),%rax
  40059b:	4c 89 c1             	mov    %r8,%rcx
  40059e:	48 89 f7             	mov    %rsi,%rdi
  4005a1:	83 e1 0f             	and    $0xf,%ecx
  4005a4:	48 29 c7             	sub    %rax,%rdi
  4005a7:	48 c1 e9 02          	shr    $0x2,%rcx
  4005ab:	4c 89 c0             	mov    %r8,%rax
  4005ae:	48 c1 ef 02          	shr    $0x2,%rdi
  4005b2:	48 f7 d9             	neg    %rcx
  4005b5:	48 83 c7 01          	add    $0x1,%rdi
  4005b9:	83 e1 03             	and    $0x3,%ecx
  4005bc:	48 39 f9             	cmp    %rdi,%rcx
  4005bf:	48 0f 47 cf          	cmova  %rdi,%rcx
  4005c3:	48 83 ff 06          	cmp    $0x6,%rdi
  4005c7:	48 0f 46 cf          	cmovbe %rdi,%rcx
  4005cb:	48 85 c9             	test   %rcx,%rcx
  4005ce:	74 20                	je     4005f0 <main+0x70>
  4005d0:	31 d2                	xor    %edx,%edx
  4005d2:	66 0f 1f 44 00 00    	nopw   0x0(%rax,%rax,1)
  4005d8:	48 83 c2 01          	add    $0x1,%rdx
  4005dc:	c7 00 00 00 00 00    	movl   $0x0,(%rax)
  4005e2:	48 83 c0 04          	add    $0x4,%rax
  4005e6:	48 39 d1             	cmp    %rdx,%rcx
  4005e9:	77 ed                	ja     4005d8 <main+0x58>
  4005eb:	48 39 cf             	cmp    %rcx,%rdi
  4005ee:	74 57                	je     400647 <main+0xc7>
  4005f0:	49 89 fa             	mov    %rdi,%r10
  4005f3:	49 29 ca             	sub    %rcx,%r10
  4005f6:	4c 89 d7             	mov    %r10,%rdi
  4005f9:	48 c1 ef 02          	shr    $0x2,%rdi
  4005fd:	4c 8d 0c bd 00 00 00 	lea    0x0(,%rdi,4),%r9
  400604:	00 
  400605:	4d 85 c9             	test   %r9,%r9
  400608:	74 2e                	je     400638 <main+0xb8>
  40060a:	66 0f ef c0          	pxor   %xmm0,%xmm0
  40060e:	49 8d 0c 88          	lea    (%r8,%rcx,4),%rcx
  400612:	31 d2                	xor    %edx,%edx
  400614:	0f 1f 40 00          	nopl   0x0(%rax)
  400618:	48 83 c2 01          	add    $0x1,%rdx
  40061c:	66 0f 7f 01          	movdqa %xmm0,(%rcx)
  400620:	48 83 c1 10          	add    $0x10,%rcx
  400624:	48 39 fa             	cmp    %rdi,%rdx
  400627:	72 ef                	jb     400618 <main+0x98>
  400629:	4d 39 ca             	cmp    %r9,%r10
  40062c:	4a 8d 04 88          	lea    (%rax,%r9,4),%rax
  400630:	74 15                	je     400647 <main+0xc7>
  400632:	66 0f 1f 44 00 00    	nopw   0x0(%rax,%rax,1)
  400638:	c7 00 00 00 00 00    	movl   $0x0,(%rax)
  40063e:	48 83 c0 04          	add    $0x4,%rax
  400642:	48 39 c6             	cmp    %rax,%rsi
  400645:	75 f1                	jne    400638 <main+0xb8>
  400647:	31 c0                	xor    %eax,%eax
  400649:	c3                   	retq   
  40064a:	66 0f 1f 44 00 00    	nopw   0x0(%rax,%rax,1)
// 進歩的な範圍for文
#include <vector>

std::vector<int> v(100);

int main() {
	for (auto &e : v) {
		e = 0;
	}
}
0000000000400580 <main>:
  400580:	4c 8b 05 d9 0a 20 00 	mov    0x200ad9(%rip),%r8        # 601060 <v>
  400587:	48 8b 35 da 0a 20 00 	mov    0x200ada(%rip),%rsi        # 601068 <v+0x8>
  40058e:	49 39 f0             	cmp    %rsi,%r8
  400591:	0f 84 b0 00 00 00    	je     400647 <main+0xc7>
  400597:	49 8d 40 04          	lea    0x4(%r8),%rax
  40059b:	4c 89 c1             	mov    %r8,%rcx
  40059e:	48 89 f7             	mov    %rsi,%rdi
  4005a1:	83 e1 0f             	and    $0xf,%ecx
  4005a4:	48 29 c7             	sub    %rax,%rdi
  4005a7:	48 c1 e9 02          	shr    $0x2,%rcx
  4005ab:	4c 89 c0             	mov    %r8,%rax
  4005ae:	48 c1 ef 02          	shr    $0x2,%rdi
  4005b2:	48 f7 d9             	neg    %rcx
  4005b5:	48 83 c7 01          	add    $0x1,%rdi
  4005b9:	83 e1 03             	and    $0x3,%ecx
  4005bc:	48 39 f9             	cmp    %rdi,%rcx
  4005bf:	48 0f 47 cf          	cmova  %rdi,%rcx
  4005c3:	48 83 ff 06          	cmp    $0x6,%rdi
  4005c7:	48 0f 46 cf          	cmovbe %rdi,%rcx
  4005cb:	48 85 c9             	test   %rcx,%rcx
  4005ce:	74 20                	je     4005f0 <main+0x70>
  4005d0:	31 d2                	xor    %edx,%edx
  4005d2:	66 0f 1f 44 00 00    	nopw   0x0(%rax,%rax,1)
  4005d8:	48 83 c2 01          	add    $0x1,%rdx
  4005dc:	c7 00 00 00 00 00    	movl   $0x0,(%rax)
  4005e2:	48 83 c0 04          	add    $0x4,%rax
  4005e6:	48 39 d1             	cmp    %rdx,%rcx
  4005e9:	77 ed                	ja     4005d8 <main+0x58>
  4005eb:	48 39 cf             	cmp    %rcx,%rdi
  4005ee:	74 57                	je     400647 <main+0xc7>
  4005f0:	49 89 fa             	mov    %rdi,%r10
  4005f3:	49 29 ca             	sub    %rcx,%r10
  4005f6:	4c 89 d7             	mov    %r10,%rdi
  4005f9:	48 c1 ef 02          	shr    $0x2,%rdi
  4005fd:	4c 8d 0c bd 00 00 00 	lea    0x0(,%rdi,4),%r9
  400604:	00 
  400605:	4d 85 c9             	test   %r9,%r9
  400608:	74 2e                	je     400638 <main+0xb8>
  40060a:	66 0f ef c0          	pxor   %xmm0,%xmm0
  40060e:	49 8d 0c 88          	lea    (%r8,%rcx,4),%rcx
  400612:	31 d2                	xor    %edx,%edx
  400614:	0f 1f 40 00          	nopl   0x0(%rax)
  400618:	48 83 c2 01          	add    $0x1,%rdx
  40061c:	66 0f 7f 01          	movdqa %xmm0,(%rcx)
  400620:	48 83 c1 10          	add    $0x10,%rcx
  400624:	48 39 fa             	cmp    %rdi,%rdx
  400627:	72 ef                	jb     400618 <main+0x98>
  400629:	4d 39 ca             	cmp    %r9,%r10
  40062c:	4a 8d 04 88          	lea    (%rax,%r9,4),%rax
  400630:	74 15                	je     400647 <main+0xc7>
  400632:	66 0f 1f 44 00 00    	nopw   0x0(%rax,%rax,1)
  400638:	c7 00 00 00 00 00    	movl   $0x0,(%rax)
  40063e:	48 83 c0 04          	add    $0x4,%rax
  400642:	48 39 c6             	cmp    %rax,%rsi
  400645:	75 f1                	jne    400638 <main+0xb8>
  400647:	31 c0                	xor    %eax,%eax
  400649:	c3                   	retq   
  40064a:	66 0f 1f 44 00 00    	nopw   0x0(%rax,%rax,1)

どれもgcc4.7.2で-O3をかけてあります。最初の方の比較のとこ以外は基本的には同じバイナリを吐く樣です。サイズも同じですし、別に氣にしなくていいのではないですかね。