对于这个例子,我正在使用
objective-c,但欢迎来自更广泛的C/C++社区的答案.
- @interface BSWidget : NSObject {
- float tre[3];
- }
- @property(assign) float* tre;
.
- - (void)assignToTre:(float*)triplet {
- tre[0] = triplet[0];
- tre[1] = triplet[1];
- tre[2] = triplet[2];
- }
.
- - (void)copyToTre:(float*)triplet {
- memcpy(tre,triplet,sizeof(tre) );
- }
所以在这两种方法之间,并考虑到这些setter函数通常只能处理2,3或4的维数……
对于这种情况,最有效的方法是什么?
gcc一般会将这些减少到相同的基本操作吗?
谢谢.
解决方法
快速测试似乎表明编译器在优化时会将memcpy调用替换为执行赋值的指令.
反编译以下代码,当未经优化编译并使用-O2时,表明在优化的情况下testMemcpy函数不包含对memcpy的调用.
- struct test src = { .a=1,.b='x' };
- void testMemcpy(void)
- {
- struct test *dest = malloc(sizeof(struct test));
- memcpy(dest,&src,sizeof(struct test));
- }
- void testAssign(void)
- {
- struct test *dest = malloc(sizeof(struct test));
- *dest = src;
- }
未经优化的testMemcpy,具有预期的memcpy调用
- (gdb) disassemble testMemcpy
- Dump of assembler code for function testMemcpy:
- 0x08048414 <+0>: push %ebp
- 0x08048415 <+1>: mov %esp,%ebp
- 0x08048417 <+3>: sub $0x28,%esp
- 0x0804841a <+6>: movl $0x8,(%esp)
- 0x08048421 <+13>: call 0x8048350 <malloc@plt>
- 0x08048426 <+18>: mov %eax,-0xc(%ebp)
- 0x08048429 <+21>: movl $0x8,0x8(%esp)
- 0x08048431 <+29>: movl $0x804a018,0x4(%esp)
- 0x08048439 <+37>: mov -0xc(%ebp),%eax
- 0x0804843c <+40>: mov %eax,(%esp)
- 0x0804843f <+43>: call 0x8048340 <memcpy@plt>
- 0x08048444 <+48>: leave
- 0x08048445 <+49>: ret
优化testAssign
- (gdb) disassemble testAssign
- Dump of assembler code for function testAssign:
- 0x080483f0 <+0>: push %ebp
- 0x080483f1 <+1>: mov %esp,%ebp
- 0x080483f3 <+3>: sub $0x18,%esp
- 0x080483f6 <+6>: movl $0x8,(%esp)
- 0x080483fd <+13>: call 0x804831c <malloc@plt>
- 0x08048402 <+18>: mov 0x804a014,%edx
- 0x08048408 <+24>: mov 0x804a018,%ecx
- 0x0804840e <+30>: mov %edx,(%eax)
- 0x08048410 <+32>: mov %ecx,0x4(%eax)
- 0x08048413 <+35>: leave
- 0x08048414 <+36>: ret
优化的testMemcpy不包含memcpy调用
- (gdb) disassemble testMemcpy
- Dump of assembler code for function testMemcpy:
- 0x08048420 <+0>: push %ebp
- 0x08048421 <+1>: mov %esp,%ebp
- 0x08048423 <+3>: sub $0x18,%esp
- 0x08048426 <+6>: movl $0x8,(%esp)
- 0x0804842d <+13>: call 0x804831c <malloc@plt>
- 0x08048432 <+18>: mov 0x804a014,%edx
- 0x08048438 <+24>: mov 0x804a018,%ecx
- 0x0804843e <+30>: mov %edx,(%eax)
- 0x08048440 <+32>: mov %ecx,0x4(%eax)
- 0x08048443 <+35>: leave
- 0x08048444 <+36>: ret