Solved

C# Improve Calculation Performance

Posted on 2011-09-12
7
324 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
  • 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

861 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