Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Efficiency (http://www.velocityreviews.com/forums/t268373-efficiency.html)

js 07-15-2003 09:31 PM

Efficiency
 
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



Ioannis Vranos 07-15-2003 09:47 PM

Re: Efficiency
 
"js" <jb.simon@lmco.com> wrote in message
news:bf1rrm$m7n1@cui1.lmms.lmco.com...
> 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


John Harrison 07-16-2003 05:55 AM

Re: Efficiency
 

"js" <jb.simon@lmco.com> wrote in message
news:bf1rrm$m7n1@cui1.lmms.lmco.com...
> 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



Gianni Mariani 07-16-2003 06:16 AM

Re: Efficiency
 
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.


Michael Kochetkov 07-16-2003 11:10 AM

Re: Efficiency
 

"js" <jb.simon@lmco.com> wrote in message
news:bf1rrm$m7n1@cui1.lmms.lmco.com...
> 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.



Pete Becker 07-16-2003 12:21 PM

Re: Efficiency
 
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

Michael Kochetkov 07-16-2003 02:07 PM

Re: Efficiency
 

"Pete Becker" <petebecker@acm.org> wrote in message
news:3F154350.DBE527C2@acm.org...
> 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.



Michael Kochetkov 07-16-2003 02:21 PM

Re: Efficiency
 

"Michael Kochetkov" <Michael.Kochetkov@trustworks.commmm> wrote in message
news:3f15624c@news.telekom.ru...
>
> "Pete Becker" <petebecker@acm.org> wrote in message
> news:3F154350.DBE527C2@acm.org...
> > 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.



Gianni Mariani 07-16-2003 02:30 PM

Re: Efficiency
 
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.


Michael Kochetkov 07-16-2003 02:44 PM

Re: Efficiency
 

"Gianni Mariani" <gi2nospam@mariani.ws> wrote in message
news:bf3ni7$35d@dispatch.concentric.net...
> 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.




All times are GMT. The time now is 06:26 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.