Jump to content

  • Log In with Google      Sign In   
  • Create Account


Which method is more effective?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 Tolito   Members   -  Reputation: 256

Like
0Likes
Like

Posted 03 February 2013 - 06:52 PM

I am working on an animation system and have an animation format and everything made. I am storing the animation database in an array (unsigned char). There are two ways I just can't choose between.

 

Frame return_frame(unsigned int animation_number, unsigned char frame_number) {
        Frame out;
        // Array here (unsigned char) containing animation database.
        // Code here that applies rotations to the variable, "out," from the frame, "frame_number," of the animation, "animation_number."
        return(out); }

 

I would have to call the above function each time I wanted to draw a frame. There are no global variables being used here as the database is declared each time the function is called.

 

The other method was to have a variable type, "Animation," which has a Frame variable in it named "frame." All animations would be declared globally and part of the program would loop through the database and fill them.

 

Which is more effective? Should I go with using a function each time that returns a frame or should I have all animations and their frames already loaded in memory? Either way, I am going to be using a function that combines different parts of different frames and returns a frame, if that information is useful.

 

Also, should I have an animation table that points to the start of each animation relative to the start of the animation database so the return_frame function does not have to go through and look for the "End Animation" command however many times in order to get to the animation that it is looking for or what? Should the same be done for frames? Does it matter?

 

Thank you! :)



Sponsor:

#2 NewDisplayName   Members   -  Reputation: 351

Like
1Likes
Like

Posted 03 February 2013 - 09:54 PM

Between the 2 methods you suggest, use the first method i.e. go with using a function each time that returns a frame.

IMHO, both of them are not the best ways to go about this problem.



#3 Tolito   Members   -  Reputation: 256

Like
0Likes
Like

Posted 04 February 2013 - 09:38 AM

Thank you for your input! That method isn't slower or rougher on the processor, is it? That was actually the one I was leaning towards. Which way would you go about doing this? And should I have an animation and frame table or is it just fine to go through the animation database, counting as it goes until it reaches the animation and frame that it is looking for? Thanks again!


Edited by Tolito, 04 February 2013 - 09:38 AM.


#4 SimonForsman   Crossbones+   -  Reputation: 5771

Like
2Likes
Like

Posted 04 February 2013 - 10:46 AM

I'd recommend against returning a structure as it would cause a rather unnecessary copy to be made(This can get quite expensive if the structure is large)(Unless C compilers are allowed/able to optimize this (Someone correct me if i'm wrong)), it would probably be better to pass a "out" frame by reference to the function and modify it in place instead and only return a error code in case the function fails for some reason.


Edited by SimonForsman, 04 February 2013 - 10:47 AM.

I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

#5 Tolito   Members   -  Reputation: 256

Like
0Likes
Like

Posted 04 February 2013 - 11:51 AM

Thank you for that information! I actually planned to call the function to return a frame as an argument to the function that will draw it. Will that be a problem? Do the copies stack up over time?



#6 Khatharr   Crossbones+   -  Reputation: 2821

Like
1Likes
Like

Posted 04 February 2013 - 12:27 PM

What he's talking about is the fact that when you return a complex type such as a struct the object has to be copied from its old context into the new one. It's not like a value which can just be set directly. They won't 'stack up' because the struct inside the function will be destroyed when the function exits. The reason you want to avoid it is for performance, but in C++ you'd also want to avoid it because copying objects can trigger complex and possibly destructive behaviors.

 

If you want to use a function to populate a struct then something like this is common in C:

 

typedef struct {
  int apeCount;
  int bananaIndex;
} Monkies;
 
void doSomething(Monkies* monks) {
  monks.apeCount = 8;
  monks.bananaIndex = 3;
}
 
int main() {
  Monkies my_monkies;
  doSomething(&my_monkies);
}

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#7 Tolito   Members   -  Reputation: 256

Like
0Likes
Like

Posted 04 February 2013 - 02:44 PM

Thank you for the example code! I should have been a little more specific. I was referring to them stacking up if not called inside a function, like so:

Frame frame=return_frame(0,0);
display(frame);

When called inside a function, it will terminate automatically indeed. What I like about your example code is that the function you declared does not need to return a variable in order to modify the variable. Does that mean it would be better to do this...

Frame frame;
return_frame(&frame,0,0);
display(frame);

Instead of this?

display(return_frame(0,0));

Very helpful advice! I need to go back and check for code that is returning structures!



#8 King Mir   Members   -  Reputation: 1910

Like
4Likes
Like

Posted 04 February 2013 - 03:14 PM

I'd recommend against returning a structure as it would cause a rather unnecessary copy to be made(This can get quite expensive if the structure is large)(Unless C compilers are allowed/able to optimize this (Someone correct me if i'm wrong)), it would probably be better to pass a "out" frame by reference to the function and modify it in place instead and only return a error code in case the function fails for some reason.

This is called Return Value Optimisation, and C compilers can do it, including gcc. Still, it doesn't hurt to do it explicitly.



#9 Tolito   Members   -  Reputation: 256

Like
0Likes
Like

Posted 05 February 2013 - 12:29 PM

Very helpful information, everyone! Thank you for your time! :)



#10 Khatharr   Crossbones+   -  Reputation: 2821

Like
0Likes
Like

Posted 06 February 2013 - 07:58 PM

LOL, the thread is tagged "C Language" but you're using C++ there.

I find it useful to use return codes for indicating error states or information about what the function did. Handling objects by reference or pointer not only frees up the return value but allows you a consistent means for when you want to pass more than one object.

Another thing to consider in cases like this - and I'm not implying that you don't already have it right in this case - is whether this function should be a member of the class/struct or remain as an outside function.

Edited by Khatharr, 06 February 2013 - 08:11 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#11 ultramailman   Prime Members   -  Reputation: 1557

Like
0Likes
Like

Posted 06 February 2013 - 11:27 PM

LOL, the thread is tagged "C Language" but you're using C++ there.

That's puzzling, after reading over all the posts, I still can't find hints that this is c++ they are talking about.

#12 Khatharr   Crossbones+   -  Reputation: 2821

Like
-2Likes
Like

Posted 07 February 2013 - 08:58 AM


Frame frame;
return_frame(&frame,0,0);
display(frame);



void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#13 nobodynews   Crossbones+   -  Reputation: 1764

Like
0Likes
Like

Posted 07 February 2013 - 10:05 AM

 

Frame frame;
return_frame(&frame,0,0);
display(frame);

 

 

I don't see the C++ there at all. I even made a C program to try it out and it compiled correctly.


Edited by nobodynews, 07 February 2013 - 10:07 AM.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!


#14 Khatharr   Crossbones+   -  Reputation: 2821

Like
0Likes
Like

Posted 08 February 2013 - 10:50 AM

Never mind. For some reason I saw the & and thought reference instead of pointer-to. Possibly an advanced case of herp-derp. I've been spending so much time on schoolwork lately (eng and mat pre-reqs) that I haven't been doing any actual coding. -.-
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS