Which method is more effective?

Started by
12 comments, last by Khatharr 11 years, 2 months ago

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

Advertisement

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.

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!

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.

[size="1"]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!

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?

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.

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!

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.

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

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.
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.

This topic is closed to new replies.

Advertisement