[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 343
  • Last Modified:

C# Improve Calculation Performance

Hi

I have this program usually it takes about 843 msec. I need to improve the performance. Please let me know if you can help with a few tips.

 static void Main(string[] args)
        {
            DateTime t0 = DateTime.Now;
            double sum = 0;
            for (int i = 0; i < 10000; i++)
            {
                for (int j = 0; j < 1000; j++)
                {
                    double x = Math.Pow(i, 2) * 0.125;
                    double y = j * 3.14159 + j + 2;
                    sum += x / y;
                }
            }
            TimeSpan ts = DateTime.Now - t0;
            Console.WriteLine("Result = {0} time= {1} msec", sum, ts.Milliseconds);
            Console.ReadKey();
        }

Open in new window


Thanks
0
JoseHidalgo
Asked:
JoseHidalgo
  • 4
  • 2
1 Solution
 
käµfm³d 👽Commented:
There's not much point in having

double x = Math.Pow(i, 2) * 0.125;

Open in new window


inside the inner loop since j isn't involved. I would move that just above the inner loop. The compiler may optimize this anyway, but you may see a gain if instead of storing the calculation to x and y you just add the whole calculation to sum.

You may want to consider using TotalMilliseconds rather than Milliseconds. I believe there is a difference in those two properties based on your time calculation.
0
 
käµfm³d 👽Commented:
Actually, disregard my second comment with respect to x. That contradicts my first comment. You would move the x part to outside of the loop, but you would refactor line 10 to be a part of line 11.
0
 
Jacques Bourgeois (James Burger)Commented:
You are creating 2000 new variables, because you declare x and y inside of the loop. Each loop creates 2 new variables. While declaring value objects (Double is a value object) does not have the same impact as declaring reference object (based on a class), it does come into play.

Move the declarations of x and y before the loop and simply use the variables inside the loop.

Even better, do not use the x and y variables at all:
sum += (Math.Pow(i, 2) * 0.125) / (j * 3.14159 + j + 2);
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
käµfm³d 👽Commented:
Even better, do not use the x and y variables at all:
Hey! That's what I said  ; )

You are creating 2000 new variables, because you declare x and y inside of the loop.
That's debatable. The compiler may optimize that out, and I recall reading a discussion on S.O. that said that would not be the case. I do not remember the link to the question though.
0
 
Jacques Bourgeois (James Burger)Commented:
You are creating 2000 new variables

Hey! That's what I said ; )

That's debatable.

There is still the old trick, very old trick. Try it to see if it is faster. In such a simple case, that would be faster than a debate :-)
0
 
käµfm³d 👽Commented:
In such a simple case, that would be faster than a debate :-)
Trust me, I'm not trying to be antagonistic. Benchmarking code is always subject to scrutiny. And it's always dependent on the environment in which the benchmark is performed. Hence the ever present debate any time asks a question like, "how can I make this faster." "Sure it's fast on my system and slow on yours, but I don't have full-blown SQL Server running on my machine either..."  That sort of thing. I agree, it generally comes down to testing it for one's self rather than asking the community.
0
 
JoseHidalgoAuthor Commented:
Thanks
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now