Any possiblity to avoid costy construction of temporary?

  • Permalink
  • submit to reddit
  • Email
  • Follow


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
Reply Sebastian 3/23/2008 7:42:58 PM

See related articles to this posting

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
Reply 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
Reply Carl 3/23/2008 11:23:15 PM
comp.lang.c++.moderated 10584 articles. 6 followers. Post

2 Replies
183 Views

Similar Articles

[PageSpeed] 22


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

avoiding temporaries
Dear group, I would like to ask if the following code is legal C++ and whether NRVO optimization is applicable at operator=(). Consider the following example: #include <new> class Vector { public: Vector() { } Vector( const Vector & v ) { for( int i = 0; i != 20; i++ ) { data[ i ] = v.data[ i ]; } } Vector( const Vector & u, const Vector & v ) // Addition { for( int i = 0; i != 20; i++ ) { data[ i ] = u.data[ i ] + v.data[ i ]; } ...

Copy Construction and temporaries
I came across a document outlining the "Programming Research Group"'s "High integrity C++ Coding Standard Manual": http://www.codingstandard.com/HICPPCM/index.html and an example for a particular rule of theirs gave me pause: http://www.codingstandard.com/HICPPCM/Rule_3.1.2.html In it, they claim that using assignment syntax to construct an object is not the same as using constructor syntax. The example they give is: string a1 = "Hello"; string b1 = string("Hello"); string c1("Hello"); and they go on to claim that in the construction ...

Possible to avoid RTTI?
Given this situation: class Base { public: virtual ~Base(); virtual bool equals(Base const&) const = 0; }; Base::~Base() {} inline bool operator==(const Base& a, const Base& b) { return a.equals(b); } class Derived : public Base { public: bool equals(const Base& b) const { if (typeid(*this) == typeid(b)) { return data_ == b.data_; } return false; } private: int data_; }; // in use Base& a(getA()); Base& b(getB()); return a == b; Is there any way I can do this without RTTI? I was thinking of somet...

temporary require ,is it possible?
I have a idea,it is "temporary require" [code] module Find # my define end tmp_require("find"){ #do something with "Find.find" method,temporary!! } puts Find # my define,no "Find.find" method [/code] Is it possible? -- Posted via http://www.ruby-forum.com/. On Thu, May 24, 2007 at 06:03:22PM +0900, Gpy Good wrote: > I have a idea,it is "temporary require" > [code] > module Find > # my define > end > > tmp_require("find"){ > #do something with "Find.find" method,temporary!! > } > >...

Avoid HTTPS when possible?
After the recent CA desasters, I wonder if one should avoid HTTPS whenever possible, in order not to create a false sense of security. I registered a .NAME domain some time ago and put some contact information there, including OpenPGP key and e-mail addresses. I was thinking of putting the URL with 'https' prefix on my visiting cards. But then I would like to give visitors the opportunity to check whether the site they are connected to is in fact run by the person who gave them the visiting card. So I would put the SSL fingerprint of my webserver on the visiting card, in order...

avoiding temporary and filesort
Hi, I have the following query: select uid,count(uid) from A group by uid; The explain plain informs me that it is using temporary and filesort. Is there a way to optimize this query without having to use file sort or temporary? Is it possible to use a view with two column, uid and count. Every time the uid column is updated, the count coulumn gets incremented? Thanks. On 8 Dec, 16:38, tekion <tek...@gmail.com> wrote: > Hi, > I have the following query: > > select uid,count(uid) from A group by uid; > > The explain plain informs me that it is using temporary and &...

How to avoid a temporary table
Hello, Often I have seen opinions in this group that temporary tables are not necessary and can always be avoided. I don't like (the oracle way of) temporary tables and would like to avoid them, if possible. However I feel that often they are unavoidable in order to "modularize" SQL, i.e. avoid multiple occurrance of similar pieces of SQL. I know that you can always rewrite a temporary table as a subquery. Often however, you end up using multiple subqueries that are very similar, i.e. duplication of "code", that you avoid in "normal" programming languages b...

Is it possible to avoid the save a PDF?
Hi, excuse my english (I'm Italian) I have created a website and there's a page in which I would like to show a pdf file but I would like to protect from saving and copying. Is it possible? If yes, how? Thank you "bettina68" <bettina68@yahoo.it>, In a message on Sat, 13 May 2006 13:12:57 +0200, wrote : "> Hi, excuse my english (I'm Italian) "> "> I have created a website and there's a page in which I would like to show a "> pdf file but I would like to protect from saving and copying. "> Is it possible? If ye...

possible to avoid a shadow merge?
I have a new ALPHA as a satellite in my cluster. The idea is to boot it up only when CSWB is being used, as the other systems don't have the resources. (The machine is too power-hungry to leave on all the time.) I have SYSUAF etc on a non--system-disk shadow set. As a result, this disk can't be dismounted before shutting down the satellite (at least not easily), due to open files. Under what conditions will shutting down and rebooting the satellite cause a merge on this disk (or any other disks it can't dismount due to open files (assuming the applications with the open f...