Efficiency

P

Petr Maxa

Michael said:
<snip>

OK, it's me the OP.

That is excatly the question. What is the correct way to code this so that
the storage is only allocated once.

That is what I meant by efficient. Granted it doesn't take long either
way,

even if as someone else pointed out it is only a few
nano seconds.
I do not like having things that should be constants allocated and/or
assigned over and over.

Sounds like the right way is to make the const array a class attribute,
and

then access it from within the member function.

I was trying to keep it local because that is the only place it is used.

In case the const char hexdigits [17] = "0123456789ABCDEF" ; declaration is
global or local static then there is a chance it would be equivalent to just
const char* str = "0123456789ABCDEF";. The key point is _const_ in const
char hexdigits []declaration. If it was char hexdigits [] a compiler would
have to copy the contents of literal string into hexdigits by all means
(whether hexdigits global, static or local). You shall realize that array
is a contaiter and you are usually supposed to change its contents whilest
leaving the original sample along. But the const makes it a little different
and a compiler may optimize the array initialization off for a global or
local static variable. In some cases I believe it may do it for local
(automatic) variable, but it may appear to be very unreliable optimization
and I doubt there is a compiler that does it.
IMO, in your case the most efficient ways would be:
1. Make use of the string literal directly (if you use it from the single
place it may appear to be the best choice)
cout << "0123456789ABCDEF";
2. Declare global, global static (depricated, use unnamed namespace instead)
pointer. It may be enforced by some corporate standards of coding:
static const char* str = "0123456789ABCDEF";
namespace {
const char* str = "0123456789ABCDEF";
}
const char* str = "0123456789ABCDEF";
3. Declare local static pointer
void f () {
static const char* str = "0123456789ABCDEF";
}
4. Declare local pointer (it shall be initialized each time the program
enters the f's scope, but a compiler could optimize it off easily)
void f () {
const char* str = "0123456789ABCDEF";
}

And I could hadly give a prove to using array of const chars in your case. A
professional programmer that may look at your code is used to think that
you, another professional programmer, would hardly complicate the code for
no particular reason. He would probably look for the perpose for such a
construction. And such unusual solutions makes the code harder to read and
understand. I wonder if this thread may serve as the illustration of what I
say. And, please, note, that all of this is just my opinion and it is not a
kind of obligatory rules.
I must disagree with You little bit. You must realize one feature of
C++'s global const objects - they are static by default, so when you
declare outside function:

const char hexastring[] = "0123456789ABCDEF"; /* static by default in C++ */

this is different, when You do the same inside a function:

void f()
{
const char hexastring[] = "0123456789ABCDEF"; /* not static !! */
}

So these two program fragment should be the same efficient:

/* 1st version - the most efficient */
const char hexastring[] = "0123456789ABCDEF"; /* static by default in C++ */
void f()
{
std::cout << hexastring;
}

/* 2nd version - also the most efficient */
void f()
{
std::cout << "0123456789ABCDEF";
}

But the following should be little bit slower, because, local const will
be copied inside the function each time it is run :

/* 3rd version - less efficient */
void f()
{
const char hexastring[] = "0123456789ABCDEF";
std::cout << hexastring;
}

Maybe with good optimization the 3rd version might be as effective as
the 1st and 2nd, moreover because the initializer of the hexastring is
constant.

Petr.
 
P

Petr Maxa

Petr Maxa wrote:

/* 3rd version - less efficient */
void f()
{
const char hexastring[] = "0123456789ABCDEF";
std::cout << hexastring;
}

Maybe with good optimization the 3rd version might be as effective as
the 1st and 2nd, moreover because the initializer of the hexastring is
constant.

Petr.

Now I know why the compiler in the 3rd version does copying each time
the function runs - according to the ARM, $2.5.4, page 11, strings
literals are of type char[] and not const char[] for compatibility with
classic C!!!

Petr
 
M

Michael Kochetkov

Petr Maxa said:
Michael said:
<snip>

I am absolutely agree with you. And in general, or generally speaking or
upon the whole the second variant is slower because in general hexdigits

may

appear to be local.

<snip>

OK, it's me the OP.

That is excatly the question. What is the correct way to code this so that
the storage is only allocated once.

That is what I meant by efficient. Granted it doesn't take long either
way,

even if as someone else pointed out it is only a few
nano seconds.
I do not like having things that should be constants allocated and/or
assigned over and over.

Sounds like the right way is to make the const array a class attribute,
and

then access it from within the member function.

I was trying to keep it local because that is the only place it is used.

In case the const char hexdigits [17] = "0123456789ABCDEF" ; declaration is
global or local static then there is a chance it would be equivalent to just
const char* str = "0123456789ABCDEF";. The key point is _const_ in const
char hexdigits []declaration. If it was char hexdigits [] a compiler would
have to copy the contents of literal string into hexdigits by all means
(whether hexdigits global, static or local). You shall realize that array
is a contaiter and you are usually supposed to change its contents whilest
leaving the original sample along. But the const makes it a little different
and a compiler may optimize the array initialization off for a global or
local static variable. In some cases I believe it may do it for local
(automatic) variable, but it may appear to be very unreliable optimization
and I doubt there is a compiler that does it.
IMO, in your case the most efficient ways would be:
1. Make use of the string literal directly (if you use it from the single
place it may appear to be the best choice)
cout << "0123456789ABCDEF";
2. Declare global, global static (depricated, use unnamed namespace instead)
pointer. It may be enforced by some corporate standards of coding:
static const char* str = "0123456789ABCDEF";
namespace {
const char* str = "0123456789ABCDEF";
}
const char* str = "0123456789ABCDEF";
3. Declare local static pointer
void f () {
static const char* str = "0123456789ABCDEF";
}
4. Declare local pointer (it shall be initialized each time the program
enters the f's scope, but a compiler could optimize it off easily)
void f () {
const char* str = "0123456789ABCDEF";
}

And I could hadly give a prove to using array of const chars in your case. A
professional programmer that may look at your code is used to think that
you, another professional programmer, would hardly complicate the code for
no particular reason. He would probably look for the perpose for such a
construction. And such unusual solutions makes the code harder to read and
understand. I wonder if this thread may serve as the illustration of what I
say. And, please, note, that all of this is just my opinion and it is not a
kind of obligatory rules.
I must disagree with You little bit. You must realize one feature of
C++'s global const objects - they are static by default, so when you
declare outside function:

const char hexastring[] = "0123456789ABCDEF"; /* static by default in C++
*/
Nonsense. Provided your hexastring is declared in the global scope then it
has external linkage. If it was static as you say it would have internal
linkage.
And more over I have not considered const char hexastring[] in my last post
at all.
As for global declaration
const char* str = "0123456789ABCDEF";
then I am afraid you mix it up with
const char* const str = "0123456789ABCDEF";

Please, make sure you understand the difference between a pointer to const
and const pointer.
 
P

Petr Maxa

Michael said:
I must disagree with You little bit. You must realize one feature of
C++'s global const objects - they are static by default, so when you
declare outside function:

const char hexastring[] = "0123456789ABCDEF"; /* static by default in C++

*/
Nonsense. Provided your hexastring is declared in the global scope then it
has external linkage. If it was static as you say it would have internal
linkage.

I must insist on my word, global const object have internal linkage.
From ARM, $7.1.6, p. 110:

"In C++, the default linkage of a const object is internal. In ANSI C,
default storage class of a const object is extern. Naturally, explicitly
declaring a const object extern will provide behavior equivalent to ANSI
C, as the following:

extern const MIN = -1;

......"

You can find the same in TC++L from Stroustrup.

Petr.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top