Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Avoid wasting time or how to avoid initialization

Reply
Thread Tools

Avoid wasting time or how to avoid initialization

 
 
Alexander Malkis
Guest
Posts: n/a
 
      04-07-2004
A real-life example:

int alpha_beta(unsigned depth,Position p, Move& m /*,other args*/) {
//...do smth with p
if(depth) {
Move m;
int val=alpha_beta(depth-1,p,m /*,other args*/);
}
//...
//sometimes change m, e.g.:
if(depth==global_depth) m=best_move;
//...return...
}

The problem here is that the (perhaps, default) constructor for m is
called. It may do as little as he wish, but nevertheless he inits all
the members of Move. It's time-consuming. But we don't need initializing
at all, since the "Move m" declaration is needed only to let the
recursively called fucntion sometimes change m.

In C, if M were a struct, no init were performed and we had no problem.
But what to do in C++ without too much hack?

--
Best regards,
Alex.

PS. To email me, remove "loeschedies" from the email address given.
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      04-07-2004

"Alexander Malkis" <(E-Mail Removed)-sb.de> wrote in message
news:c51d7t$153h$(E-Mail Removed)-saarland.de...
> A real-life example:
>
> int alpha_beta(unsigned depth,Position p, Move& m /*,other args*/) {
> //...do smth with p
> if(depth) {
> Move m;
> int val=alpha_beta(depth-1,p,m /*,other args*/);
> }
> //...
> //sometimes change m, e.g.:
> if(depth==global_depth) m=best_move;
> //...return...
> }
>
> The problem here is that the (perhaps, default) constructor for m is
> called. It may do as little as he wish, but nevertheless he inits all
> the members of Move. It's time-consuming. But we don't need initializing
> at all, since the "Move m" declaration is needed only to let the
> recursively called fucntion sometimes change m.
>
> In C, if M were a struct, no init were performed and we had no problem.
> But what to do in C++ without too much hack?


What's in Move?

If Move is a POD type (the only type you are allowed in C) then
initialisation will not happen in C++ either. Something like this

struct Move
{
char from_square;
char to_square;
char captured_piece;
char promoted_piece;
};

isn't going to get initialised in either C or C++.

john


 
Reply With Quote
 
 
 
 
Leor Zolman
Guest
Posts: n/a
 
      04-07-2004
On Wed, 07 Apr 2004 19:19:26 +0200, Alexander Malkis
<(E-Mail Removed)-sb.de> wrote:

>A real-life example:
>
>int alpha_beta(unsigned depth,Position p, Move& m /*,other args*/) {
>//...do smth with p
> if(depth) {
> Move m;
> int val=alpha_beta(depth-1,p,m /*,other args*/);
> }
>//...
>//sometimes change m, e.g.:
>if(depth==global_depth) m=best_move;
>//...return...
>}
>
>The problem here is that the (perhaps, default) constructor for m is
>called. It may do as little as he wish, but nevertheless he inits all
>the members of Move. It's time-consuming. But we don't need initializing
>at all, since the "Move m" declaration is needed only to let the
>recursively called fucntion sometimes change m.
>
>In C, if M were a struct, no init were performed and we had no problem.
>But what to do in C++ without too much hack?


If, in C, there were "no init" performed, you'd have garbage in the struct.

What does Move look like? Is it a POD (plain old data) type? If so, and
you don't care whether the POD data members get initialized or not
(presumably there's something in the logic of your code that would know not
to actually look into the object in that case, since you're saying that was
the case in C), then the constructor, such as it is, would be "trivial" and
optimized away to nothing.

If, on the other hand, Move is not POD and there needs to be a constructor,
then you're doing something differently than you did in C anyway, and you
probably wouldn't /want/ the initialization skipped...
-leor


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
 
Reply With Quote
 
bartek
Guest
Posts: n/a
 
      04-07-2004
Alexander Malkis <(E-Mail Removed)-sb.de> wrote in
news:c51d7t$153h$(E-Mail Removed)-saarland.de:

> A real-life example:
>
> int alpha_beta(unsigned depth,Position p, Move& m /*,other args*/) {
> //...do smth with p
> if(depth) {
> Move m;
> int val=alpha_beta(depth-1,p,m /*,other args*/);
> }
> //...
> //sometimes change m, e.g.:
> if(depth==global_depth) m=best_move;
> //...return...
> }
>
> The problem here is that the (perhaps, default) constructor for m is
> called. It may do as little as he wish, but nevertheless he inits all
> the members of Move. It's time-consuming. But we don't need

initializing
> at all, since the "Move m" declaration is needed only to let the
> recursively called fucntion sometimes change m.
>
> In C, if M were a struct, no init were performed and we had no problem.
> But what to do in C++ without too much hack?
>


I don't quite understand the rationale... Is the 'Move& m' meant as an
output argument?

Anyway... why don't you use a pointer to Move then? (or better ... a
smart pointer?) You could then construct an instance at the point it's a
ctually needed.

Cheers!
 
Reply With Quote
 
Alexander Malkis
Guest
Posts: n/a
 
      04-07-2004
//Here is the Move. It's not chess,
//but an edge-moving game which is a bit complex here to explain.
//Edge is also a class.

class Move {
public:
Edge from, to; //where do we take an edge and where do we place it
//constructor
Move(Edge from_e, Edge to_e): from(from_e), to(to_e) { }
//default-constructor
Move() { }
class ErrorBadInput { }; //the caller has to eat the input itself
friend std:stream& operator<<(std:stream&, const Move&); //output
friend std::istream& operator>>(std::istream&, Move&); //input
};

/* So as far as I understood, changing it to struct would suffice. The
problem is that the other (nondefault) constructor should be removed
also. It works but is not quite what I want, since the class is going to
grow and will sooner or later need some more member functions.

In my example, I really want to avoid initialization in this case and
"garbage" in m is ok:
*/
//...
if(depth) {
Move m;
int val=alpha_beta(depth-1,p,m /*,other args*/);
}
//...
/* The idea of a pointer gets quite messy, since the Move object would
be destroyed not in the call of alpha_beta that has created it, in
general not in alpha_beta at all. And the additional logic has to be
implemented to tell when to delete an m and when not, which has nothing
to do with algorithm itself.
*/
int alpha_beta(..., Move* &m) {
....
Move *m;
int val=alpha_beta(depth-1,p,m /*,other args*/);
if(m)...
else ...
....
if(...) m=new Move(...); else m=NULL;
}
/*
The smart pointer implies (as far I understand these) an time-overhead.
*/

--
Best regards,
Alex.

PS. To email me, remove "loeschedies" from the email address given.
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      04-08-2004

"Alexander Malkis" <(E-Mail Removed)-sb.de> wrote in message
news:c51kc8$17nf$(E-Mail Removed)-saarland.de...
> //Here is the Move. It's not chess,
> //but an edge-moving game which is a bit complex here to explain.
> //Edge is also a class.
>
> class Move {
> public:
> Edge from, to; //where do we take an edge and where do we place it
> //constructor
> Move(Edge from_e, Edge to_e): from(from_e), to(to_e) { }
> //default-constructor
> Move() { }
> class ErrorBadInput { }; //the caller has to eat the input itself
> friend std:stream& operator<<(std:stream&, const Move&); //output
> friend std::istream& operator>>(std::istream&, Move&); //input
> };
>
> /* So as far as I understood, changing it to struct would suffice. The
> problem is that the other (nondefault) constructor should be removed
> also. It works but is not quite what I want, since the class is going to
> grow and will sooner or later need some more member functions.
>


Struct make no difference at all.

Give Edge a similar constructor to Move and I would expect a compiler to be
smart enough to optimise away both constructor calls.

john


 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      04-08-2004
On Wed, 07 Apr 2004 19:19:26 +0200, Alexander Malkis
<(E-Mail Removed)-sb.de> wrote:

>A real-life example:
>
>int alpha_beta(unsigned depth,Position p, Move& m /*,other args*/) {
>//...do smth with p
> if(depth) {
> Move m;
> int val=alpha_beta(depth-1,p,m /*,other args*/);
> }


I assume you can't just do:

if(depth) {
int val=alpha_beta(depth-1,p,m /*,other args*/);
}

i.e. just pass on the move from the level above? That would solve the
problem...

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      04-08-2004

> >
> >int alpha_beta(unsigned depth,Position p, Move& m /*,other args*/) {
> >//...do smth with p
> > if(depth) {
> > Move m;
> > int val=alpha_beta(depth-1,p,m /*,other args*/);
> > }

>
> I assume you can't just do:
>
> if(depth) {
> int val=alpha_beta(depth-1,p,m /*,other args*/);
> }
>
> i.e. just pass on the move from the level above? That would solve the
> problem...
>


That's what I thought you'd want, too. I'm confused by your declaring a
local m when one of the parameters is also named m. That's an indicator of
something wrong, usually.

-Howard




 
Reply With Quote
 
Alexander Malkis
Guest
Posts: n/a
 
      04-13-2004
tom_usenet wrote:
> On Wed, 07 Apr 2004 19:19:26 +0200, Alexander Malkis
> <(E-Mail Removed)-sb.de> wrote:
>
>
>>A real-life example:
>>
>>int alpha_beta(unsigned depth,Position p, Move& m /*,other args*/) {
>>//...do smth with p
>> if(depth) {
>> Move m;
>> int val=alpha_beta(depth-1,p,m /*,other args*/);
>> }

>
>
> I assume you can't just do:
>
> if(depth) {
> int val=alpha_beta(depth-1,p,m /*,other args*/);
> }
>
> i.e. just pass on the move from the level above? That would solve the
> problem...
>
> Tom

Good idea. This would change the other code parts a bit, but it seems to
be fine enough for spped.

--
Best regards,
Alex.

PS. To email me, remove "loeschedies" from the email address given.
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Article: "Hi jackasses, RTFM and stop wasting our time trying to help you children learn." Danno Java 55 04-25-2006 03:47 PM
Re: Am I Wasting My Time? The Poster Formerly Known as Kline Sphere MCSD 5 05-02-2004 05:36 PM
OT: Master Programmer; was "Am I Wasting My Time" UAError MCSD 3 04-08-2004 06:49 PM
Re: Am I Wasting My Time? David MCSD 1 04-05-2004 12:22 PM



Advertisments