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 8926133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:53:38+00:00 2026-06-15T07:53:38+00:00

Can anyone tell me if there is any performance benefit to assigning an object

  • 0

Can anyone tell me if there is any performance benefit to assigning an object in the container to a local variable if its used a lot in a tight loop.

I have a large for loop and inside the loop an object from a container is access often.
i.e

for i := 0 to 100000 do
begin
  my_list[i].something := something;
  my_list[i].something_else := something;
  my_list[i].something_else := something;
  my_list[i].something_else := something;
  my_list[i].something_else := something;
end;

Would I see a performance improvement by assigning

local_ref := my_list[i];

at the start of each iteration?
I am using a generic container (TList<<>MyObject<>>).

  • 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-15T07:53:40+00:00Added an answer on June 15, 2026 at 7:53 am

    Making the change you suggest will certainly result in faster code. Accessing a local variable is always going to be faster than accessing a property getter on TList<T>. For a start, those getters perform validity checking on the index. But even for a class with the most simple getter possible, it would be hard to beat the cached local for performance.

    Now, whether this matters in your case is impossible to say from here. If you do anything remotely non-trivial inside the loop then their runtime of the item getter will be irrelevant.
    The fact that the loop might run for a large number of iterations is not the key factor. What counts most of all is how much time you spend in each iteration. If it costs you 1 time unit to call the item getter, and 1000 time units to do whatever you do with each item, then it the getter performance is a non-issue.

    Ultimately the definitive way to answer the question is to time the alternatives. Only optimise based on measurement.

    There’s a much better reason to copy the item into a local variable: clarity of expression. Your current code is an egregious violation of the DRY principle.

    Finally, this code would read best of all if it used a for in loop:

    for Item in List do
      ....
    

    Now, for in loops can be slower than traditional loops, but you should weight that against clarity and maintainability. Optimisation usually makes code harder to maintain and more prone to faults. Conclusion: only optimise bottlenecks.

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

Sidebar

Related Questions

Can anyone please tell me is there any special requirement to use either EXTERN
Question 1 Can anyone tell me if there is any difference between following 2
Can anyone who has used three.js tell me if its possible to detect webgl
can anyone tell me if there is any alternative for visual asssit? We are
Can anyone please tell me if there is any way we can find out
Can anyone tell me if there's any difference between using runOnUiThread() versus Looper.getMainLooper().post() to
Can anyone tell me if there is a way to select a video from
Can anyone tell me what is causing the space below the images? There seems
Can anyone tell me what is the difference between XSDObjectGen.exe & XSD.exe? Is there
Can anyone tell me if there's a way to run a thread and give

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.