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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:41:30+00:00 2026-05-24T19:41:30+00:00

The post I am referring to is this. Not only does the first one

  • 0

The post I am referring to is this. Not only does the first one run faster for me (563 MS compared to 630) when increasing the size to 100000000, but also in the past using properties in for loops has caused considerable speed slowdowns.

Once when writing a waveform viewer every frame took 23 milliseconds on average to process. The for loop that looped through the entire picture’s pixels was the cause of the slowdown. Instead of storing the picture’s width and height before the for loop I accessed it during each iteration. Upon changing it the time to process a single frame went from 23 milliseconds to a mere 3.

Also, I made this sample class:

class LOL
{
    private int x;

    public LOL(int x)
    {
        this.x = x;
    }

    public int X
    {
        get
        {
            return x;
        }
    }
}

I then made a for loop that iterates 500000000 times. One test stored the X in an integer variable before the loop even started and one accessed the X property during each iteration. The former took approximately 1,500 milliseconds and the latter around 8,000.

Three tests and each concludes with the result that storing the limit before-hand being the optimal solution for performance gains. Am I missing something? Currently in my program optimization is required as large pictures are processed and in all performance-critical areas I always store the bounds of the loop before hand for performance gains and these tests seem to confirm that.

  • 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-24T19:41:31+00:00Added an answer on May 24, 2026 at 7:41 pm

    Compiled in release mode, the cached length is faster. On debug it is the opposite.

    Test method

    public static class Performance
    {
        public static void Test(string name, decimal times, bool precompile, Action fn)
        {
            if (precompile)
            {
                fn();
            }
    
            GC.Collect();
            Thread.Sleep(2000);
    
            var sw = new Stopwatch();
    
            sw.Start();
    
            for (decimal i = 0; i < times; ++i)
            {
                fn();
            }
    
            sw.Stop();
    
            Console.WriteLine("[{0,15}: {1,-15}]", name, new DateTime(sw.Elapsed.Ticks).ToString("HH:mm:ss.fff"));
            Debug.WriteLine("[{0,15}: {1,-15}]", name, new DateTime(sw.Elapsed.Ticks).ToString("HH:mm:ss.fff"));
        }
    }
    

    Test code:

    var testAmount = 100;
    
    int[] a1 = new int[10000000];
    int[] a2 = new int[10000000];
    
    Performance.Test
    (
        "Direct", testAmount, true,
        () =>
        {
            for (int i = 0; i < a1.Length; ++i)
            {
                a1[i] = i;
            }
        }
    );
    
    Performance.Test
    (
        "Cache", testAmount, true,
        () =>
        {
            var l = a2.Length;
            for (int i = 0; i < l; ++i)
            {
                a2[i] = i;
            }
        }
    );
    

    Debug results

    [   Direct: 00:00:06.474   ]
    [   Cache:  00:00:06.907   ]
    

    Release results

    [   Direct: 00:00:05.382   ]
    [   Cache:  00:00:04.714   ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

referring to yesterday's post, this woke me up this morning. Why does this actually
I'm referring to a problem similar to the one in this post , because
Referring to this post and this one . I'm trying to implement tag search
First post, so here goes. I'm writing a script that does intelligent search and
This post reference to the One Definition Rule. Wikipedia is pretty bad on explaining
First, a quote from the ole' manual on ArrayAccess::offsetSet() : This function is not
this is my very first post! I'm pretty desperate so I'm going above and
this is my first post. I have this function for reversing a string in
first of all this is my first post so maybe the whole description may
Post-release, I have made one small change to one form in our development site

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.