Solved

C# Improve Calculation Performance

Posted on 2011-09-12
7
318 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 74

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 74

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 74

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 74

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Software development teams often use in-memory caches to improve performance. They want to speed up access to, or reduce load on, a backing store (database, file system, etc.) by keeping some or all of the data in memory.   You should implement a …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now