Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6182241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:09:57+00:00 2026-05-24T01:09:57+00:00

I’m programming a game in the C# XNA framework and something has been bugging

  • 0

I’m programming a game in the C# XNA framework and something has been bugging me for quite some time. I’m never certain whether it’s to the advantage of a program to save data (that is to say, store it within a local variable) or to calculate it each frame.

While I’ve decided that it’s better to continuously calculate data that changes each frame (such as the position of the player, the positions of automated sprites, et cetera), I’m uncertain whether or not I should save a piece of data like the width of a window or calculate it each frame. My fear is that calculating a piece of data, regardless of how small it may be, sixty times every second will generate quite a bit of overhead, so thus far I’ve been sticking to saving a lot of material.

Here’s an example of what I’m trying to get across:

//  Scenario 1: Save the data

//  This class represents an entry within a menu.
public class MenuEntry
{
    //  The constructor takes a SpriteFont variable to calculate the size of 
    //  the given string (which is the text used to represent this entry in a 
    //  menu) and save that data for later access
    public MenuEntry(string entryText, SpriteFont font)
    {
        this.entryText = entryText;

        //  Initializes a local value of the font to lessen the burden of method calls.
        SpriteFont font = menuScreen.GameInstance.MenuFont;

        //  Initialize the bounding rectangle for this 'MenuEntry' object.
        entryRectangle = new Rectangle();

        //  Based on the text given, adjust the values of the rectangle.
        entryRectangle.Height = font.LineSpacing;
        entryRectangle.Width = (int)font.MeasureString(entryText).X;
    }

    //  Object methods

    //  This method simply sets the location of the 'hitbox' of this MenuEntry based
    //  on the given position.
    public void SetLocation(int x, int y)
    {
        entryRectangle.X = x;
        entryRectangle.Y = y;
    }

    //  Object fields

    //  Rectangle that represents the 'hitbox' for this MenuEntry.  In other
    //  words, the place on the screen over which the mouse can hover to cause
    //  this MenuEntry to be the selected item on the menu
    private Rectangle entryRectangle;

    //  The 'entryText' is the string representation of this MenuEntry object in
    //  a menu.
    private string entryText;
}

//  Scenario 2: Calculate per frame

//  This class also represents an entry within a menu.
public class MenuEntry
{
    //  The constructor simply initializes the value of the text field
    public MenuEntry(string entryText)
    {
        this.entryText = entryText;
    }

    //  Object methods

    //  Instead of saving the data, these methods allow this MenuEntry
    //  to calculate its 'hitbox' whenever this method is called, 
    //  returning the width and height variables associated with the
    //  text representation of this MenuEntry object.
    public virtual int GetWidth(SpriteFont font)
    {
        return (int)font.MeasureString(Text).X;
    }

    public virtual int GetHeight(SpriteFont font)
    {
        return font.LineSpacing;
    }

    //  Object fields

    //  The MenuEntry still must know where it's located, but this component
    //  is less memory intensive than a 'Rectangle' object (correct me if
    //  I'm wrong on this one)
    private Vector2 position;

    //  This property allows for the controlling menu to place and replace the
    //  MenuEntry, though this value must be calculated each frame even though
    //  the position may be the same for each frame.
    public Vector2 Position
    {
        get { return position; }
        set { position = value; }
    }

    //  The 'entryText' is the string representation of this MenuEntry object in
    //  a menu.
    private string entryText;
}

//  Example in menu:

public class Menu
{
    //  For the sake of simplicity, the constructor simply receives a list of entries
    //  and initializes them to a local variable.  If we're using Scenario 1, the  
    //  positions for each rectangle must be set.
    public class Menu(SpriteFont menuFont, List<MenuEntry> menuEntries)
    {
        this.menuFont = menuFont;
        this.menuEntries = menuEntries;

        #if Scenario 1

        Vector2 position = new Vector2(0f, 175f);

        for(int i = 0; i < menuEntries.Count; i++)
        {
            //  Initialize a local reference to the current MenuEntry.
            MenuEntry menuEntry = menuEntries[i];

            //  Adjust this entry toward the center of the screen.  Assume it
            //  has access to a static viewport variable.
            position.X = Game.Viewport.Width / 2 - menuEntry.GetWidth(this) / 2;

            //  Set the entry's position to the calculated position.  There must be
            //  casts as these are floating-point values.
            menuEntry.SetLocation((int)position.X, (int)position.Y);

            //  Move down for the next entry the size of this entry
            position.Y += menuEntry.GetHeight(this);
        }

        #endif
    }

    //  This method is called by the main update method once per frame.
    public void Update(GameTime gameTime)
    {
        #if Scenario 1

        //  Nothing, as the MenuEntry items don't need to move and their positions
        //  have already been set.

        #elif Scenario 2

        Vector2 position = new Vector2(0f, 175f);

        for(int i = 0; i < menuEntries.Count; i++)
        {
            //  Initialize a local reference to the current MenuEntry.
            MenuEntry menuEntry = menuEntries[i];

            //  Adjust this entry toward the center of the screen.  Assume it
            //  has access to a static viewport variable.
            position.X = Game.Viewport.Width / 2 - menuEntry.GetWidth(this) / 2;

            //  Set the entry's position
            menuEntry.Position = position;

            //  Move down for the next entry the size of this entry
            position.Y += menuEntry.GetHeight(this);
        }

        #endif
    }

    //  Object fields

    //  The list of MenuEntry items associated with this Menu.
    private List<MenuEntry> menuEntries;

    //  The font used for this Menu.
    private SpriteFont menuFont;
}

Though the example is a bit long, I feel as though it adequately encapsulates my question. So, is it better to save more data to an object (in the case of the example, save a rectangle structure) or is it better to calculate this data each frame? Note that the data I’m speaking of doesn’t change between frames.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:09:58+00:00Added an answer on May 24, 2026 at 1:09 am

    60 times per second in nothing for simple calculations. Your computer can do several million simple calculations per second.

    Chose the one that produces the easier to maintain code. And since windows can be resized I strongly recommend recalculating window based stuff like aspect ratio, projection matrix etc on each frame.

    Such performance concerns are only relevant for code that gets executed very often. Say >1 million times a second.

    For rare execution only expensive calls such as IO matter.

    So I’m with svick on this issue: Profile to find the bottleneck and then optimize it. For all other code use the clearest simplest version.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.