You are not logged in.
I am reading The C Programming Book (K&R), and I was reading about declaring variables (advising? the compiler to use a variable from the register).
I am curious as to why it isn't used more. I have read a lot of code but I haven't seen it used much, like:
register int x; // heavy used variable
I made a simple program that does roughly 80 million simple calculations (addition and subtraction). I made two functions, one using the register declaration and the other without using the register. I wanted to time both, doing the same calculations. The results:
[ZXXX@Archlinux ~]$ ~/Programming/c/a.out
Finish time for yesReg = 5 seconds
Finish time for noReg = 15 seconds
Finish time for yesReg = 5 seconds
Finish time for noReg = 14 seconds
Clearly using the register is much faster for calculations/loops. Why isn't it used more? Is it bad practice or are there some standards for using them?
Last edited by Google (2010-08-03 13:14:23)
Offline
I love C, but had never heard of the "register" keyword before now. After doing a search, it seems that modern compilers are supposed to automatically choose to do the same optimization thing that the "register" keyword would do. But, after seeing the results from your program, that obviously isn't the case.
http://wiki.answers.com/Q/What_is_the_u … yword_in_C
If you are able to add all of the super strict compile time options to GCC and not have any errors or warnings and your code runs faster, I can't see any reason not to use it.
Offline
Using register too often will eventually cause your code to slow down. You'll be forcing unnecessary swaps to move around data that doesn't really warrant optimization for fast access.
If you want to know more about this, start up reading up on assembly and CPU architecture.
Offline
Thanks, the more I read the more I need to read. I started with The C Programming Book, and then I started reading Operating Systems Concepts, and then I started to read Code Complete, and Unix Systems Programming.. and now I need to add some ASM and architecture books.
Good thing I enjoy reading and learning
Offline
Normally it will be optimized by the compiler... But you can use register variable, for tiny loop ( for 1..9 for example)
Offline
Do you mind posting the code you used for the benchmark? This is interesting. Also, was it compiled with optimization (-O{1,2,3})?
Last edited by tavianator (2010-08-03 16:03:27)
Offline
Almost everything about register variables is implementation-defined. The 'register' keyword is just a suggestion to the compiler that a particular variable will be used a lot, and so it might be a good idea to store the variable in a register rather than in main memory. You can make as many variables 'register' as you want, but the compiler is free to ignore any or all of them as it pleases.
You can't take the address of a 'register' variable, and 'register' isn't even guaranteed to do anything interesting. There are only a limited number of registers available, so hinting to the compiler that some variable should be placed in a register might theoretically result in forcing some other variable counterproductively out into main memory.
Code would be nice. I'd also like to note that programs almost never do nothing but calculation, so in a program where you're not just sitting in a tight loop flipping bits, the benefit of using 'register' is likely to be much less noticeable.
Offline
In CPU architecture, there's few registers which is quite fast using for particularly cases. I don't C often, C++ in windows so I don't have quite depth understanding about programming. But It seems register like "inline" in windows programming, it makes your code clearly faster but it's not much so use it carefully . [ fix me if I'm wrong ]
Thanks, the more I read the more I need to read. I started with The C Programming Book, and then I started reading Operating Systems Concepts, and then I started to read Code Complete, and Unix Systems Programming.. and now I need to add some ASM and architecture books.
Very nice reading mate. I'm reading them too. Code complete is quite good but it's a little overwhelm for a single-person coding
When you live for a strong purpose, then hard work isn't an option. It's a necessity. - Steve Pavlina
dotFiles
Offline
Using register too often will eventually cause your code to slow down. You'll be forcing unnecessary swaps to move around data that doesn't really warrant optimization for fast access.
If you want to know more about this, start up reading up on assembly and CPU architecture.
Actually enforcing the use of the register keyword is up to the compiler. Just because you put it in your code, the compiler will only put that variable in a register if it thinks it should. After all, the compiler knows better than you anyway
Offline
Actually enforcing the use of the register keyword is up to the compiler. Just because you put it in your code, the compiler will only put that variable in a register if it thinks it should. After all, the compiler knows better than you anyway
Sure. If you want to get detailed, any code you feed a compiler (particularly gcc) is nothing but a "hint" as to how the program should be translated to machine code.
Offline
In CPU architecture, there's few registers which is quite fast using for particularly cases. I don't C often, C++ in windows so I don't have quite depth understanding about programming. But It seems register like "inline" in windows programming, it makes your code clearly faster but it's not much so use it carefully . [ fix me if I'm wrong ]
You're wrong but not by much "inline" and "register" are both implemented only at the compiler's discretion. At higher optimization levels this probably means that the compiler will ignore both keywords and optimize as it sees fit. Even with no optimization, they're only suggestions, and they place semantic constraints on your code even if the compiler ignores them.
Use both keywords judiciously. "inline" won't save you much over only two or three function calls. "register" won't save you much unless the variable is heavily used (for computation). Disk I/O trumps pretty much everything, so in an I/O bound process neither will do you any good. What's more, the compiler is allowed (and in some cases encouraged) to ignore them. Finally, both represent a form of manual optimization, and should be treated the same way -- profile the code first, test both with and without, and stick with the simpler version if the more complex one doesn't give a noticeable speed increase.
Offline
Sure. If you want to get detailed, any code you feed a compiler (particularly gcc) is nothing but a "hint" as to how the program should be translated to machine code.
No. Well, yes, but some hints are more optional than others. Take a keyword like "static" -- for any compiler to be standard-compliant, it must treat a static block-scope variable differently from a non-static one. "register" is different because the standard itself states that the compiler need not heed it (and within your code, as long as it's carefully written, you can't tell the difference).
Of course, gcc in its default mode is a special and non-standard dialect of C, so the standard doesn't exactly apply.
Offline
No. Well, yes, but some hints are more optional than others.
A fair point, and too much of an over-generalization on my part. The standard does dictate a small set of rules which must be obeyed. Call me jaded after reading too many rants by Linus about how gcc mangled his code.
Offline
I used to frequent the cprogramming.com forums and I've heard it all about compiler optimisations etc from very pedantic C89/90 programmers.
In fact I was told (long ago) that the register variable is almost useless which is why drcouzelis said he has never heard of it before because these days nobody really finds a need for it.
You can also do inline assembly in C but again, if you think you know better than the compiler, go for it. Otherwise just code in C and be done with it.
Last edited by sand_man (2010-08-04 23:49:39)
Offline
Do you mind posting the code you used for the benchmark? This is interesting. Also, was it compiled with optimization (-O{1,2,3})?
Sorry for my late reply, I have been reading more and more and testing a lot. I don't have the original benchmark program I made, I kept tweaking it and checking out how the compiler reacted with different types of uses for register and optimizations.
The default GCC O1, O2 and O3 seem to optimize to the point where my benchmarking program didn't provide enough weight to keep the cpu busy, I tested it with optimization and the program was finishing in less than a second. I was mostly testing with the default gcc output, because that was where I could feel the speed differences and able to clock them. I assume even an optimized program will work well with register variables as well, it's just harder to benchmark
I think this topic has brought up some interesting talk so I won't mark it as solved, unless a mod or someone thinks it should be solved. I think there could be a lot more interesting talk about this, it's interesting to me! I may be a bit nerdy though
Last edited by Google (2010-08-05 02:37:32)
Offline
I assume even an optimized program will work well with register variables as well, it's just harder to benchmark
Usually you repeat the computation many times and report an average or feed it with more data when its possible.
Offline
I assume even an optimized program will work well with register variables as well, it's just harder to benchmark
Bad assumption, since the whole point of optimization is to have the compiler do what you're trying to do by hand.
Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.
Offline
I seriously doubt that using 'register' will help when using optimization; gcc's optimization passes put a lot of thinking into register allocation. I'm not surprised that it helped the performance of unoptimized code though; without -O, gcc generates terribly slow code that's really easy to debug. For the one CPU-bound application I'm writing right now, the difference between -O0 and -O2 is a factor of 9 for the main loop, and a factor of 6 overall.
If you're interested in benchmarking, I'm going to shamelessly plug my benchmarking library, libsandglass, which provides convenient clock-cycle resolution timing on x86 and x86-64 CPUs.
Offline