f



Any possiblity to avoid costy construction of temporary?

I have a hash class which is expensive to create but are often used
for comparsion. Consider the following code:

[code]
void SomeFunc( Hash someHash )
{
     if( Hash("SomeLengthyString") == someHash )
     {
          ...
     }
}
[/code]

Hash("SomeLengthyString") is expensive to construct and will stay
constant for all calls to SomeFunc. I understand that the compiler
isn't allowed to cache Hash("SomeLengthyString") through calls since
it's a performance / memory trade off, and from the compilers pov the
constructor of Hash might have additional dependencies. I would get
the expected behaviour if I would make Hash("SomeLengthyString")
static, which led me to the following code, which even compiles under
MSVC2005:

[code]
void SomeFunc( Hash someHash )
{
     if( static Hash("SomeLengthyString") == someHash )
     {
          ...
     }
}
[/code]

However the constructor of Hash is still called once for every
comparsion.

Regards,
Sebastian Karlsson

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Sebastian
3/23/2008 7:42:58 PM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8506) is leader. Post Follow

2 Replies
366 Views

Similar Articles

[PageSpeed] 40

Sebastian Karlsson wrote:

> Hash("SomeLengthyString") is expensive to construct and will stay
> constant for all calls to SomeFunc. I understand that the compiler
> isn't allowed to cache Hash("SomeLengthyString") through calls since
> it's a performance / memory trade off, and from the compilers pov the
> constructor of Hash might have additional dependencies. I would get
> the expected behaviour if I would make Hash("SomeLengthyString")
> static, which led me to the following code, which even compiles under
> MSVC2005:
> 
> [code]
> void SomeFunc( Hash someHash )
> {
>      if( static Hash("SomeLengthyString") == someHash )
>      {
>           ...
>      }
> }
> [/code]
> 
> However the constructor of Hash is still called once for every
> comparsion.
> 


g++ does not accept that construct at all. Complaining:

<snip>
error: expected primary-expression before �static�
</snip>

Seems like you're constructing a static temporary variable,
since there is no object name only the constructor Hash(...).

Anyway. If I do:

<snip>
....
static Hash h("SomeLengthyString");
if( h == someHash) {
....
}
</snip>

it behaves like expected. Sometimes dense expressions
like declaration and comparison in one line is evil.
Although I like it too :-).

Hope that helps.

O.



-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Oncaphillis
3/23/2008 11:22:39 PM
In article
<f3e582e0-25bf-4b2c-b507-159559353f44@e23g2000prf.googlegroups.com>,
Sebastian Karlsson <Sebastian.Karlsson@mmorpgs.org> wrote:

> I have a hash class which is expensive to create but are often used
> for comparsion. Consider the following code:
> 
> [code]
> void SomeFunc( Hash someHash )
> {
>      if( Hash("SomeLengthyString") == someHash )
>      {
>           ...
>      }
> }
> [/code]
> 
> [code]
> void SomeFunc( Hash someHash )
> {
       static Hash  const_hash("SomeLengthyString");
       if( const_hash == someHash)
       ...
}
> 
> However the constructor of Hash is still called once for every
> comparsion.
>

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Carl
3/23/2008 11:23:15 PM
Reply: