Solved

C# Improve Calculation Performance

Posted on 2011-09-12
7
329 Views
Last Modified: 2012-05-12
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
Comment
Question by:JoseHidalgo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 36526737
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36526742
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
 
LVL 40
ID: 36526813
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36526825
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
 
LVL 40
ID: 36526860
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36526874
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
 

Author Closing Comment

by:JoseHidalgo
ID: 36527126
Thanks
0

Featured Post

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question