Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# Improve Calculation Performance

Posted on 2011-09-12
7
Medium Priority
?
340 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 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Learn how ViaSat reduced average response times for IT incidents from 10 minutes to 30 seconds.
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

618 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