Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Efficiency

Reply
Thread Tools

Efficiency

 
 
js
Guest
Posts: n/a
 
      07-15-2003
for a hex conversion, Is it more efficient to do this :

Printit is the char whose low order bits represent the nibble to print

cout << "0123456789ABCDEF" [ Printit ] ;

or :

const char hexdigits [17] = "0123456789ABCDEF" ;

cout << hexdigits [ Printit ] ;


Or doesn't it matter ?

Thanks
Joe


 
Reply With Quote
 
 
 
 
Ioannis Vranos
Guest
Posts: n/a
 
      07-15-2003
"js" <(E-Mail Removed)> wrote in message
news:bf1rrm$(E-Mail Removed)...
> for a hex conversion, Is it more efficient to do this :
>
> Printit is the char whose low order bits represent the nibble to print
>
> cout << "0123456789ABCDEF" [ Printit ] ;
>
> or :
>
> const char hexdigits [17] = "0123456789ABCDEF" ;
>
> cout << hexdigits [ Printit ] ;



There is and a third method:


const char *hexdigits="0123456789ABCDEF";

cout << hexdigits [Printit];



It doesn't really matter. Use the one that is more convenient to you.







--
Ioannis

* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-16-2003

"js" <(E-Mail Removed)> wrote in message
news:bf1rrm$(E-Mail Removed)...
> for a hex conversion, Is it more efficient to do this :
>
> Printit is the char whose low order bits represent the nibble to print
>
> cout << "0123456789ABCDEF" [ Printit ] ;
>
> or :
>
> const char hexdigits [17] = "0123456789ABCDEF" ;
>
> cout << hexdigits [ Printit ] ;
>
>
> Or doesn't it matter ?


You got it. If there is any difference it will be measureable in nano
seconds. Excessively concern for 'efficiency' at the cost of considering
more important factors, such as clean design and legible code, is a typical
newbie trait.

>
> Thanks
> Joe
>


john


 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      07-16-2003
js wrote:
> for a hex conversion, Is it more efficient to do this :
>
> Printit is the char whose low order bits represent the nibble to print
>
> cout << "0123456789ABCDEF" [ Printit ] ;
>
> or :
>
> const char hexdigits [17] = "0123456789ABCDEF" ;
>
> cout << hexdigits [ Printit ] ;
>
>
> Or doesn't it matter ?



These are exactly the same.

However, you could find out for yourself by testing each one.

 
Reply With Quote
 
Michael Kochetkov
Guest
Posts: n/a
 
      07-16-2003

"js" <(E-Mail Removed)> wrote in message
news:bf1rrm$(E-Mail Removed)...
> for a hex conversion, Is it more efficient to do this :
>
> Printit is the char whose low order bits represent the nibble to print
>
> cout << "0123456789ABCDEF" [ Printit ] ;
>
> or :
>
> const char hexdigits [17] = "0123456789ABCDEF" ;

A compiler is supposed to copy "0123456789ABCDEF" into hexdigits [17] here.

>
> cout << hexdigits [ Printit ] ;

So the first variant is faster. Though such a short string may be copied
with no loops, using memory/register operations only but the long text may
consume considerable time and stack memory. I believe the last may appear to
be more important because it may lead to the runtime crash (at least on
Win32 architectures).

> Or doesn't it matter ?

You are to decide.

--
With regards,
Michael Kochetkov.


 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      07-16-2003
Michael Kochetkov wrote:
>
> > const char hexdigits [17] = "0123456789ABCDEF" ;

> A compiler is supposed to copy "0123456789ABCDEF" into hexdigits [17] here.
>


There's no such requirement. In fact this is usually done by direct
intialization of the array in the executable image, i.e. with no runtime
overhead at all.

--

"To delight in war is a merit in the soldier,
a dangerous quality in the captain, and a
positive crime in the statesman."
George Santayana

"Bring them on."
George W. Bush
 
Reply With Quote
 
Michael Kochetkov
Guest
Posts: n/a
 
      07-16-2003

"Pete Becker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Michael Kochetkov wrote:
> >
> > > const char hexdigits [17] = "0123456789ABCDEF" ;

> > A compiler is supposed to copy "0123456789ABCDEF" into hexdigits [17]

here.
> >

>
> There's no such requirement. In fact this is usually done by direct
> intialization of the array in the executable image, i.e. with no runtime
> overhead at all.

I would agree with you, but I am not a compiler or library writer so I am
not that self-confident . I have carefully checked it out before posting
and have found that at least the following compilers: MS VC6.0, MS VC7.0, MS
VC7.1, Borland 5.6, Intel C++ 7.1, DigitalMars 8.34, g++ 3.2 behave the way
I have described with maximum optimization been set.
And no doubt you are aware of the fact that the special bobs are needed to
obtain more then 4K on the stack in Win32 world.

--
With regards,
Michael Kochetkov.


 
Reply With Quote
 
Michael Kochetkov
Guest
Posts: n/a
 
      07-16-2003

"Michael Kochetkov" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Pete Becker" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Michael Kochetkov wrote:
> > >
> > > > const char hexdigits [17] = "0123456789ABCDEF" ;
> > > A compiler is supposed to copy "0123456789ABCDEF" into hexdigits [17]

> here.
> > >

> >
> > There's no such requirement. In fact this is usually done by direct
> > intialization of the array in the executable image, i.e. with no runtime
> > overhead at all.

> I would agree with you, but I am not a compiler or library writer so I am
> not that self-confident . I have carefully checked it out before posting
> and have found that at least the following compilers: MS VC6.0, MS VC7.0,

MS
> VC7.1, Borland 5.6, Intel C++ 7.1, DigitalMars 8.34, g++ 3.2 behave the

way
> I have described with maximum optimization been set.

So, I mean that in fact this is not usually done by direct initialization of
the array in the executable image at least in Win32 world.

--
With regards,
Michael Kochetkov.


 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      07-16-2003
Michael Kochetkov wrote:

[discussion trimmed]

>
> So, I mean that in fact this is not usually done by direct initialization of
> the array in the executable image at least in Win32 world.
>



void func2( int Printit )
{
cout << "0123456789ABCDEF" [ Printit ] ;
}

const char hexdigits [17] = "0123456789ABCDEF" ;

void func2( int Printit )
{
cout << hexdigits [ Printit ] ;
}


What seems to be the problem ?

These are exactly the same in terms of performance on virtually any
platform.

 
Reply With Quote
 
Michael Kochetkov
Guest
Posts: n/a
 
      07-16-2003

"Gianni Mariani" <(E-Mail Removed)> wrote in message
news:bf3ni7$(E-Mail Removed)...
> Michael Kochetkov wrote:
>
> [discussion trimmed]

It is a pity.

>
> >
> > So, I mean that in fact this is not usually done by direct

initialization of
> > the array in the executable image at least in Win32 world.
> >

>
>
> void func2( int Printit )
> {
> cout << "0123456789ABCDEF" [ Printit ] ;
> }
>
> const char hexdigits [17] = "0123456789ABCDEF" ;
>
> void func2( int Printit )
> {
> cout << hexdigits [ Printit ] ;
> }
>
>
> What seems to be the problem ?

The key words were about hexdigits [xxx] on the stack.

--
With regards,
Michael Kochetkov.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
perl efficiency -- fastest grepping? Bryan Krone Perl 1 11-08-2004 11:15 PM
Efficiency of dynamically adding web user controls MC D ASP .Net 4 11-18-2003 07:27 PM
Custom Paging Efficiency Joseph D. DeJohn ASP .Net 1 08-06-2003 06:24 PM
CISCO 1721 efficiency problem DarejDok \(Dariusz Dħbrowski\) Cisco 0 07-16-2003 07:15 AM
dataset efficiency question Trevor Hartman ASP .Net 0 07-03-2003 08:20 PM



Advertisments