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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:47:10+00:00 2026-05-24T23:47:10+00:00

Performance-wise, is it better to access an array element ‘directly’ multiple times, or assign

  • 0

Performance-wise, is it better to access an array element ‘directly’ multiple times, or assign its value to a variable and use that variable? Assuming I’ll be referencing the value several times in the following code.

The reasoning behind this question is that, accessing an array element presumably involves some computing cost each time it is done, without requiring extra space. On the other hand, storing the value in a variable eliminates this access-cost, but takes up extra space.

// use a variable to store the value
Temp = ArrayOfValues(0)
If Temp > 100 Or Temp < 50 Then
    Dim Blah = Temp
    ...

// reference the array element 'directly'
If ArrayOfValues(0) > 100 Or ArrayOfValues(0) < 50 Then
    Dim Blah = ArrayOfValues(0)
    ...

I know this is a trivial example, but assuming we’re talking about a larger scale in actual use (where the value will be referenced many times) at what point is the tradeoff between space and computing time worth considering (if at all)?

  • 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-24T23:47:11+00:00Added an answer on May 24, 2026 at 11:47 pm

    The overhead in memory consumption is very limited because for reference types it’s just a pointer (couple of bytes) and most value types also require just a few bytes.

    Arrays are very efficient structures in most languages. Getting to an index doesn’t involve any lookup but just some math (each array slot takes 4 bytes so the 11th slot is at offset 40). Then there is probably a bit of overhead for bounds checking. Allocating the memory for a new local var and freeing it requires a bit of cpu cycles as well. So in the end it also depends how many array lookups you eliminate by copying to a local var.

    Fact is that you really need exceptionally crappy hardware or have really big loops for this to be really important and if it is run a decent test on it. I personally choose often for the seperate variable as I find that it makes the code more readible.

    Your example is odd btw since you do 2 array lookups before you create the local var 🙂
    This makes more sense (elimination 2 more lookups)

    Dim blah = ArrayOfValues(0)
    if blah > 100 or blah < 50 then
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is better for performance wise declaring the variable outside the foreach statment and
A quick question, which is better performance and memory wise: <t:outputText escape=false value='<tr><td>'/> or
In ASP.NET which is better performance wise; Localization using the file based resx /
Performance wise which one is better: $(.myclass).eq(0) OR $(a.myclass) Note: If there is only
Performance wise, why and how Datacontract serializer is better than xmlserializer ?
Performance wise, what is better? If I have 3 or 4 join statements in
which is better, performance-wise, when you potentially have an empty list? if (_myList !=
I wanted to know what is better performance-wise: to put the conditions in the
Since Modernizr.load and Yepnope are asynchronous loaders, is it better performance-wise to use them
Which method is better (performance-wise) if I have DataBoundControl such as GridView, Repeater, and/or

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.