What is the performance of the โ€œaddressโ€ of the operator?

I need to pass a lot of pointers in my code in the middle of a big loop (so I have a lot of expressions like foo(&x, &y, ...) ). I was wondering if it's worth storing pointers as separate variables (like a cache) for performance (by introducing more variables and clutter in my code)?

(Doing a lot of matrix elements and the CUBLAS library insists on pointers ...)

+4
source share
4 answers

No - the carrier address is about as inexpensive / fast as anything you can hope for. You can overload it, and such an overload can be slower, but overloading is not at all quite unusual.

+10
source

std::addressof does not inflict any penalty. When it comes down to assembler, we still refer only to objects at their addresses, so the information is already at hand.

As for operator& , it depends on whether it was overloaded or not. The original, non-overloaded version behaves exactly like std::addressof . However, if operator& was overloaded (this is a very bad idea, and really didnโ€™t like it), all bets are disabled, since we cannot guess what an overloaded implementation is.

So, the answer to your question: there is no need to store pointers separately, you can just use std::addressof or operator& whenever you need it, even if you need to repeat it.

+4
source

C ++ has links, what you describe is more like behavior for the C language, not C ++.

such a signature for C ++ methods, which it usually implements to avoid copying, by default, when you call a method and pass some arguments to it, these arguments generate a local copy, referring to it as a method that will help you avoid this overhead for a copy.

+1
source

That would be very effective. If you are using Linux, you can check with objdump. The following shows what fooP (& var1, & var2) looks like in the x86 architecture. This is nothing more than a LEA instruction.

  fooP(&var1, &var2); 8048642: 8d 44 24 1c lea 0x1c(%esp),%eax -> this is to get the address of var2 to eax 8048646: 89 44 24 04 mov %eax,0x4(%esp) -> this is to save the address to the stack for later fooP call 804864a: 8d 44 24 18 lea 0x18(%esp),%eax -> address of var1 804864e: 89 04 24 mov %eax,(%esp) 8048651: e8 be ff ff ff call 8048614 <_Z4fooPPiS_> 

The link in this case is actually the same as above.

+1
source

Source: https://habr.com/ru/post/1481301/


All Articles