Jump to content

  • Log In with Google      Sign In   
  • Create Account


Most memory effective way to do this


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
3 replies to this topic

#1 Tallkotten   Members   -  Reputation: 288

Like
0Likes
Like

Posted 02 August 2012 - 06:15 AM

Hello!

I'm in need of help. I am about to build a system that displays info about an item on the screen and i need some help to understand which of 2 ways is the most memory effective.

So basically in my game i am making there are items on the ground. If the player holds down CTRL he/she gets up an UI that draws out the stats and name of the item.

I currently got a sloppy but functional system and i want to improve it. I got 2 ways in mind.

First way:

First create a object of the class in the initiation:
ItemInfo* UI = new ItemInfo();

And then when i want to draw something new i contact the build function in the class to update it.
UI->build(all the variables);

When i want to show it i switches a bool which makes it draw on the screen. I switch the bool to false when i want to hide it.

This way the UI is basically always gonna be on the screen (in the memory), just invisible to the user most times.

Second way:

When i want to draw the UI i create a new object of the class and sends all the necessary variables with it.
ItemInfo* UI = new ItemInfo(all the variables);

Then if i don't want to show it anymore i delete the object and set the pointer to NULL
delete UI;
UI = NULL;

If i want to show it again/update it i recreate it again, but with different variables. Of course if it isn't deleted already i have to do that first
ItemInfo* UI = new ItemInfo(all the variables);

I show it if the pointer != NULL, if it is NULL I don't try to draw it (since there isn't anything to draw)

This way is more memory and CPU intense when i am showing itemInfo but the memory is freed when i ain't doing anything with it.

Which way is more effective?

Sponsor:

#2 mhagain   Crossbones+   -  Reputation: 7466

Like
1Likes
Like

Posted 02 August 2012 - 06:58 AM

How often will you be displaying item info? If it's reasonably often, if there is almost always some item info on-screen, and if the item for which info is being displayed changes frequently enough, then you should probably accept the extra memory overhead, otherwise you're looking at a potentially bad run-time bottleneck.

Even if not, you should definitely be profiling memory usage to determine if you are or are not worrying overmuch about something that may not even be a problem. Even if you've got a coupla extra MB usage, this is almost totally insignificant by todays standards - the days of having to be ultra-frugal about memory usage, and when memory usage was highly likely to be a major program bottleneck, are over.

That's not an excuse for going nuts or doing stupid things; it just means that focussing solely or even primarily on memory usage is not as important as it used to be.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#3 Ashaman73   Crossbones+   -  Reputation: 6744

Like
2Likes
Like

Posted 02 August 2012 - 07:02 AM

Take the first approach. Instanciate all the widgets you need and display them ,when needed. There's no reason to create/delete them all the time.

#4 Tallkotten   Members   -  Reputation: 288

Like
0Likes
Like

Posted 02 August 2012 - 07:03 AM

How often will you be displaying item info? If it's reasonably often, if there is almost always some item info on-screen, and if the item for which info is being displayed changes frequently enough, then you should probably accept the extra memory overhead, otherwise you're looking at a potentially bad run-time bottleneck.

Even if not, you should definitely be profiling memory usage to determine if you are or are not worrying overmuch about something that may not even be a problem. Even if you've got a coupla extra MB usage, this is almost totally insignificant by todays standards - the days of having to be ultra-frugal about memory usage, and when memory usage was highly likely to be a major program bottleneck, are over.

That's not an excuse for going nuts or doing stupid things; it just means that focussing solely or even primarily on memory usage is not as important as it used to be.


Well that depends on the player. But imagine Diablo, there you look at the stats a whole lot. Or WOW, where you check your stats on items a lot as well.

Take the first approach. Instanciate all the widgets you need and display them ,when needed. There's no reason to create/delete them all the time.


Guess i'm going with the first option. I were aware that it might no make any difference since like you said there is a lot more memory to work with today, but i always feel that i want to do it "the right way" with as little sloppiness as possible Posted Image

Thanks for a quick reply you both! :)

Edited by Tallkotten, 02 August 2012 - 07:03 AM.





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