I apoligise in advance if this is an os or platform based question, I
don't know.
I was wondering how register integers (and other types of register
variables) are managed by c++. For example, on a pentium 4, there are
8 register integers in the cpu. If you define more than 8, or if there
are other programs using this space, how are the variables allocated.
This is for a simulation program that needs to be very fast.
For example:
int foo()
{
register int a;
register int b;
register int c;
register int d;
register int e;
register int f;
register int g;
register int h;
register int i;
//Code using register ints
return 1;
}
There are 9 register integers defined, and, assuming you have only 8 on
your platform, will, after you have defined more than 8, the first one
be put into normal memory or is there some other system for allocating
them.
Also, in for loops, is the counting variable by default a register int.
I have done several tests and the time for a for loop defined like
this:
for( int n = 0; n < 10000; n++ )
{
//Code
}
for( register int n = 0; n < 10000; n++ )
{
//Code
}
is the same.
Thanks
-Alex 3 2234
"Alex" <al*******@gmail.com> wrote... I apoligise in advance if this is an os or platform based question, I don't know.
It's not platform-based. It is, however, implementation-defined.
I was wondering how register integers (and other types of register variables) are managed by c++. For example, on a pentium 4, there are 8 register integers in the cpu. If you define more than 8, or if there are other programs using this space, how are the variables allocated. This is for a simulation program that needs to be very fast. For example:
int foo() { register int a; register int b; [...]
'register' just like 'inline' is not a directive, but rather a suggestion
for your compiler. It may take a hint and try placing the object in a CPU
register, or it may decide against it and do whatever it sees fit. Just
like you noticed, it's not always possible, besides, it's not always the
desired course of action.
One thing you should probably learn at this point: compilers are better at
deciding what needs to go where than programmers. So, spend your time on
_algorithms_ and getting things _right_, before ever attempting to get them
"very fast". Let the compiler decide what registers to use and when. Do
_not_ concern yourself with performance of your code on any particular CPU
type or model; instead concern yourself with portability and maintainability
of your code. IOW, forget the "register" keyword exists or what it's for,
except that you're not allowed to use it anywhere else. Trust me, your life
will be simpler that way.
Victor
Victor Bazarov wrote: "Alex" <al*******@gmail.com> wrote...
I apoligise in advance if this is an os or platform based question, I don't know.
It's not platform-based. It is, however, implementation-defined.
I was wondering how register integers (and other types of register variables) are managed by c++. For example, on a pentium 4, there are 8 register integers in the cpu. If you define more than 8, or if there are other programs using this space, how are the variables allocated. This is for a simulation program that needs to be very fast. For example:
int foo() { register int a; register int b; [...]
'register' just like 'inline' is not a directive, but rather a suggestion for your compiler. It may take a hint and try placing the object in a CPU register, or it may decide against it and do whatever it sees fit. Just like you noticed, it's not always possible, besides, it's not always the desired course of action.
One thing you should probably learn at this point: compilers are better at deciding what needs to go where than programmers. So, spend your time on _algorithms_ and getting things _right_, before ever attempting to get them "very fast". Let the compiler decide what registers to use and when. Do _not_ concern yourself with performance of your code on any particular CPU type or model; instead concern yourself with portability and maintainability of your code. IOW, forget the "register" keyword exists or what it's for, except that you're not allowed to use it anywhere else. Trust me, your life will be simpler that way.
Victor
However, one can always assist a compiler by writing code that
makes the decisions easier for the compiler.
For example, if a processor has a string search instruction, a
person may want to set up the code so that the compiler recognizes
the "pattern" and implements the string search function.
I've done this on the ARM processor using registers and its
special transfer instructions.
--
Thomas Matthews
C++ newsgroup welcome message: http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq: http://www.comeaucomputing.com/learn/faq/
Other sites: http://www.josuttis.com -- C++ STL Library book http://www.sgi.com/tech/stl -- Standard Template Library
Thomas Matthews wrote: Victor Bazarov wrote:
"Alex" <al*******@gmail.com> wrote...
I apoligise in advance if this is an os or platform based question, I don't know. It's not platform-based. It is, however, implementation-defined.
I was wondering how register integers (and other types of register variables) are managed by c++. For example, on a pentium 4, there are 8 register integers in the cpu. If you define more than 8, or if there are other programs using this space, how are the variables allocated. This is for a simulation program that needs to be very fast. For example:
int foo() { register int a; register int b; [...] 'register' just like 'inline' is not a directive, but rather a suggestion for your compiler. It may take a hint and try placing the object in a CPU register, or it may decide against it and do whatever it sees fit. Just like you noticed, it's not always possible, besides, it's not always the desired course of action.
One thing you should probably learn at this point: compilers are better at deciding what needs to go where than programmers. So, spend your time on _algorithms_ and getting things _right_, before ever attempting to get them "very fast". Let the compiler decide what registers to use and when. Do _not_ concern yourself with performance of your code on any particular CPU type or model; instead concern yourself with portability and maintainability of your code. IOW, forget the "register" keyword exists or what it's for, except that you're not allowed to use it anywhere else. Trust me, your life will be simpler that way.
Victor
However, one can always assist a compiler by writing code that makes the decisions easier for the compiler.
For example, if a processor has a string search instruction, a person may want to set up the code so that the compiler recognizes the "pattern" and implements the string search function.
I've done this on the ARM processor using registers and its special transfer instructions.
Oh, there is no doubt about it. The point[s] of my post was/were that
(a) 'register' is not the way to do that, (b) before attempting doing that
one should understand the inner workings of the compiler/CPU combination
very well and (c) portability and maintainability are often more important
than minute improvements in performance: productivity is not measured by
the performance of a single program fragment but by the overall output of
the [sometimes quite large] group of people that involves using and
advancing that program (and I am not sure if I read it somewhere or that
I just made it up :-).
Victor This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ioannis Vranos |
last post by:
Just some thought on it, wanting to see any explanations.
It was advised in this newsgroups that we should avoid the use of
keyword register.
However it is a language feature, and if it...
|
by: ajay |
last post by:
some say floats can't be stored in register.
size of int and float are same ( on some machine am i Wrong?)
so if int can be delcared as register than why not float(where in and
float are of same...
|
by: Shankar Agarwal |
last post by:
I was trying to use a variable whose value is temporary and i don't want
it to go to the memory for performance reasons. Is there a way to specify
the variable like that so that any assignment to...
|
by: aruna |
last post by:
What is the disadvantage of using register
storage class specifier?
|
by: Jackie |
last post by:
Hi everyone,
Does anyone know when "register" declarations should be used and when
"register" must not be used? If possible please give examples for both
cases.
Thanks
|
by: junky_fellow |
last post by:
what is the purpose of declaring a register variable ?
why can't we find the address of register variable ?
|
by: sanchana |
last post by:
how can i check whether a register variable is assigned to a register
only or not
|
by: orium69 |
last post by:
hi everyone, i'm wondering if there is a way to have sure that a
variable is allocated in the cache, after its declaration with
"register"? Tks!
|
by: int main(void) |
last post by:
Hi all,
I know that register variables work fine at block scope.
I tried putting a register variable in file scope, like this,
#include <stdio.h>
register int a = 2;
int main(void)
{...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |