Sign in to follow this  
Tolito

Which method is more effective?

Recommended Posts

Tolito    256

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! :)

Share this post


Link to post
Share on other sites
Tolito    256

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

Share this post


Link to post
Share on other sites
SimonForsman    7642

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

Share this post


Link to post
Share on other sites
Tolito    256

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?

Share this post


Link to post
Share on other sites
Khatharr    8812

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);
}

Share this post


Link to post
Share on other sites
Tolito    256

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!

Share this post


Link to post
Share on other sites
King Mir    2490

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.

Share this post


Link to post
Share on other sites
Khatharr    8812
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

Share this post


Link to post
Share on other sites
ultramailman    1720

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.

Share this post


Link to post
Share on other sites
nobodynews    3126

 

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

Share this post


Link to post
Share on other sites
Khatharr    8812
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. -.-

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this