What is the performance gain when I log into my variable?

Asked

Viewed 115 times

0

I’m still reading the Complete and Total C booklet, and it says that:

[...] This means that operations on the Register variables could occur much faster than on the armezanadas variables in memory, because the value of these variables was actually conserved in the CPU and no memory access was required to determine or modify their values [...]

Doesn’t it say here the performance gain that I have, does it have any chart or explanatory content? And when should I actually use it? I know that I should not abuse, and neither arrays should enter as it says (Page 47):

[...] Larger objects, such as matrices, obviously cannot be stored in a recorder [...]

Edit1: I’m quite enjoying C, I am seeing that I should have learned first because it is fundamental to see how memory works and etc... Another question, as C would be like "Parent" of other languages, this storage specifier also works in other languages like PHP, Python, JavaScript, Java?

Edit2: I saw in the comments and said that this booklet is bad and old, some suggestion for reading?

It seemed to me that information was missing to complete...

  • This book is very old and is not completely valid today. Nowadays this keyword is not so useful anymore. Modern compilers do this automatically and are also better at choosing this than we humans. Reference: https://stackoverflow.com/questions/578202/register-keyword-in-c

  • This book is really bad, really bad.

  • Vish! Really? Which book do you refer me to? And why is it so bad?

  • 2

    Congratulations on your perception, if you know how to drive well you have the potential to become a great programmer because at least you see something different and that few people see, even you can change your mind when you see something ahead, in general nowadays people are quite stubborn. I think that question has already been answered in https://answall.com/q/169857/101. That’s it, it hasn’t been needed for a long time, just like other languages don’t need or even make sense. This is not just an old book, it’s perhaps the worst ever written about C. See the [tag:c].

  • This is not a booklet it’s book piracy.

2 answers

3

The word register suggests to the compiler that this variable should be allocated in a register. This was useful in older compilers, at a time when register allocation algorithms were primitive and machines had little processing capacity, which made this process very slow since the general case of this problem is NP-complete.

Today, register allocation is almost optimally done by compilers, which makes the use of register very unnecessary, even advised against. The compiler knows by means of an analysis called Liveness Analysis, where a variable begins to exist, and where it is no longer needed. With this it can allocate and de-locate the register as needed.

Something that might be useful from the attribute register is that it is not possible to remove her address, but to this day I have never seen her used for it.

  • Remember that it is not possible to obtain the address of a variable register

2

In more practical terms, consider the simple example below:

#include <stdio.h>

int testecomregister() {
    register int x = 5;
    x = x + 2;
    x = x - 1;
    return x;
}

int testesemregister() {
    int x = 5;
    x = x + 2;
    x = x - 1;
    return x;
}

int main (void) {
    testecomregister();
    testesemregister();
    return 0;
}

Taking the proper proportions at the time of compilation to extract the debugging information, let’s:

gcc -O0 -ggdb3 teste.c -o teste

To check the output Assembly of both functions, extract them with objdump and sed to filter the output:

objdump -d teste -M intel | sed '/<testecomregister>:/,/^$/!d'

0000000000401106 <testecomregister>:
  401106:   55                      push   rbp
  401107:   48 89 e5                mov    rbp,rsp
  40110a:   53                      push   rbx
  40110b:   bb 05 00 00 00          mov    ebx,0x5
  401110:   83 c3 02                add    ebx,0x2
  401113:   83 eb 01                sub    ebx,0x1
  401116:   89 d8                   mov    eax,ebx
  401118:   5b                      pop    rbx
  401119:   5d                      pop    rbp
  40111a:   c3                      ret 


objdump -d teste -M intel | sed '/<testesemregister>:/,/^$/!d'

000000000040111b <testesemregister>:
  40111b:   55                      push   rbp
  40111c:   48 89 e5                mov    rbp,rsp
  40111f:   c7 45 fc 05 00 00 00    mov    DWORD PTR [rbp-0x4],0x5
  401126:   83 45 fc 02             add    DWORD PTR [rbp-0x4],0x2
  40112a:   83 6d fc 01             sub    DWORD PTR [rbp-0x4],0x1
  40112e:   8b 45 fc                mov    eax,DWORD PTR [rbp-0x4]
  401131:   5d                      pop    rbp
  401132:   c3                      ret  

The most noticeable difference is that in the case using Register, the operations are done directly in rbx, while in the case of another, the operations are done based on the base-offset register. For practical purposes, this is negligible these days. Remembering that the above situation is forced, when I put a level of optimization, gcc already does homework bringing me the result directly:

  401106:   b8 06 00 00 00          mov    eax,0x6
  40110b:   c3                      ret 

In other words, the compiler knows what he does. Let him work :)

  • It is possible to have GCC remove even function calls if they are declared as Static, or if the Link Time Optimization is enabled through the flag -flto.

  • Well, if I force an inline would get less didactic to answer hehehe In the case of a mere printf, the interest part would get reduced to a mere mov esi <direct value>; mov Edi <place of plt>

  • By declaring as static you’re not forcing her to be inline. You just tell the compiler that this function will not be visible to the external context, i.e., its scope is just that file. This allows GCC to break function call patterns and perform even more aggressive optimizations. Turn it into inline is just one of the things he can do. But in fact, it gets a little less didactic :-)

  • Ah, sorry. I didn’t pay attention to the "Static" because it wasn’t highlighted in the commentary. Yes, you’re right.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.