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

  • Home
  • SEARCH
  • 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 7874709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:51:27+00:00 2026-06-03T02:51:27+00:00

Is it better to assign a object property to a variable first instead of

  • 0

Is it better to assign a object property to a variable first instead of using it directly in a loop?

Say I have a bitmap Bitmap img = new Bitmap("image.jpg") and I needed to loop through all the pixels to do some processing. For a 1080p image that’s about 2 million pixels. Does it matter if I use data.Stride or assign them to a variable first int dataStride = data.Stride? I need to access it each time to calculate the offset but the dataStride is a constant of the image.

data = editImage.LockBits(new Rectangle(0, 0, editWidth, editHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
byte* dataPtr = (byte*)data.Scan0;
dataStride = data.Stride;

I assign them to a variable first since I am thinking that it has to access the object (each time) first and then access the integer from the object (each time) which is slower. And since it is a large loop… it adds up. So assigning the property to a variable first will be faster since it can access the int value directly. Is this correct?

  • 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-06-03T02:51:27+00:00Added an answer on June 3, 2026 at 2:51 am

    Yes. No matter how simple the property, accessing it still has the overhead of a function call. A variable is faster, especially if you are doing something 2 million times.

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

Sidebar

Related Questions

I have an object, and I have a new value which I need to
I have some code that assigns an object to a generic id variable and
if object has property of type NSString or NSNumber , which is better, retain
I have a data object (let's say it's called 'Entry') that has a set
don't know better title for this, but here's my code. I have class user
Lets say I have the following code: public class Base { // Some stuff
I have a graph which I traverse using a typical visitor pattern. I've run
By books first explains the problem (using ARC) about unvalid references, like in this
I have using a basic DataContext to create objects then submit these into a
How can one add a slider control object in Excel Interop using Visual Studio

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.