Thread: Speed of pointers vs. speed of arrays/structs

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    147
    I found the trick here, in the web of cprogramming.com:
    http://www.cprogramming.com/tips/sho...ount=30&page=0

    what about using a register for store the pointer of a global struct:

    Code:
    register GlobalVars *g asm(\\"a4\\");
    I found this link too: http://gcc.gnu.org/onlinedocs/gcc-4....-Reg-Vars.html

    I would like to test by myself if this provoke a speed improvement in my program, but using MINGW for windows I don't know how to do it. I get this error for the above line:

    Code:
    syntax error before asm
    Anyone knows how to do it for windows/intel plataform. Also "a4" is the name of the register to use. Any recomendation/tip on what register to use?

    Want to test it. theory is good, but in speed issues I think it is better to test by myselg it in order to make conclusions. I will post my results

    thx

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, that is more to the point that in some architectures (68000 in this example) allows you to store a pointer in a global register. For the most cases in a modern compiler, however, it just gives the compiler one less register to work with, and the compilers these days are pretty darn good at figuring out what to put in a register, and what to not put in a register.

    On x86, this would almost certainly cause more problems than it solves as there are only a few (7 at the very most) freely usable registers in x86. Even if almost every line is accessing the global variable it's unlikely to give any enormous benefits then either, since the compiler will be able to load the global pointer at the beginning of the code and then operate on it from then on.

    Compilers these days are pretty good at assigning registers the right way, and given a choice, I'd prefer to use the compiler to figure out which register to use when.

    However, it does work to use global register variables in gcc:
    Code:
    #include <stdio.h>
    
    register int x asm("ebx");
    
    
    int main()
    {
      x = 7;
      printf("x = %d\n", x);
      return 0;
    }
    compiles OK with gcc-mingw 3.4.2, and studying the assembler code, it puts the value 7 in ebx, and pushes ebx to printf.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    147
    Quote Originally Posted by matsp View Post
    Well, that is more to the point that in some architectures (68000 in this example) allows you to store a pointer in a global register. For the most cases in a modern compiler, however, it just gives the compiler one less register to work with, and the compilers these days are pretty darn good at figuring out what to put in a register, and what to not put in a register.

    On x86, this would almost certainly cause more problems than it solves as there are only a few (7 at the very most) freely usable registers in x86. Even if almost every line is accessing the global variable it's unlikely to give any enormous benefits then either, since the compiler will be able to load the global pointer at the beginning of the code and then operate on it from then on.

    Compilers these days are pretty good at assigning registers the right way, and given a choice, I'd prefer to use the compiler to figure out which register to use when.

    However, it does work to use global register variables in gcc:
    Code:
    #include <stdio.h>
    
    register int x asm("ebx");
    
    
    int main()
    {
      x = 7;
      printf("x = %d\n", x);
      return 0;
    }
    compiles OK with gcc-mingw 3.4.2, and studying the assembler code, it puts the value 7 in ebx, and pushes ebx to printf.

    --
    Mats
    I don't know why, but mingw get an error "syntax error before asm", any idea?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Kempelen View Post
    I don't know why, but mingw get an error "syntax error before asm", any idea?
    With the code I just posed, or with your post?

    My code compiles with gcc-mignw 3.4.2.

    What options are you using for gcc? If you give -ansi, then all asm-statements are syntax errors, since asm is not ansi compliant. You can still use inline assembler if you use __asm__ instead of asm, in which case my code also compiles with -ansi.

    Code:
    register int x __asm__("ebx");
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    147
    Quote Originally Posted by matsp View Post
    With the code I just posed, or with your post?

    My code compiles with gcc-mignw 3.4.2.

    What options are you using for gcc? If you give -ansi, then all asm-statements are syntax errors, since asm is not ansi compliant. You can still use inline assembler if you use __asm__ instead of asm, in which case my code also compiles with -ansi.

    Code:
    register int x __asm__("ebx");
    --
    Mats
    Hi Mats, thx for you support. At the end, I think i can't test it, because I have multiple files, and if I declare a variable as register, then it is not extern and vice-versa. Only one storage class type is allowed.
    I have test it declaring the variable as register in .h, and normal one in .c. It compile good but dont get expected result when run.
    Anyway I didn't confident I would obtein speed improvements.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can't you use the global register declaration in a header-file (that is then included everywhere)[1]? I'm pretty sure that will work. Although I still think the compiler on it's own will do a better job, so you may well be right that you won't benefit much (if anything). I'd be interested to see what happens, however.

    [1] This really shouldn't cause any problems, as there won't be a global symbol for something that the compiler makes into a register.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    147
    Quote Originally Posted by matsp View Post
    Can't you use the global register declaration in a header-file (that is then included everywhere)[1]? I'm pretty sure that will work. Although I still think the compiler on it's own will do a better job, so you may well be right that you won't benefit much (if anything). I'd be interested to see what happens, however.

    [1] This really shouldn't cause any problems, as there won't be a global symbol for something that the compiler makes into a register.

    --
    Mats
    I can use it. I was refering that the version of the program that is declared as extern is running OK and I get the expected results from the program. If I declare as register in the way writed above, I can compile, but when run the program don't show the same result as the non-register version, even a crash. dont know why.
    Also, when compile I get an error : "warning: unknow register name: reg" that I dont understand (dont know where 'reg' came from)

    (EDIT: forget to say that the crash is in a line where the global reg var is not used, so it is more extrange.....isn't it?)
    Last edited by Kempelen; 06-26-2008 at 06:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. moving pointers to pointers
    By Benzakhar in forum C++ Programming
    Replies: 9
    Last Post: 12-27-2003, 08:30 AM
  3. pointers to pointers within structs
    By Lord_azrael99 in forum C Programming
    Replies: 2
    Last Post: 08-28-2003, 04:29 AM
  4. Pointers pointers pointers....
    By G'n'R in forum C Programming
    Replies: 11
    Last Post: 11-02-2001, 02:02 AM
  5. Pointers pointers pointers...
    By SMurf in forum C Programming
    Replies: 8
    Last Post: 10-23-2001, 04:55 PM