Slow method calls

Are the methods really too slow or is there something wrong with my computer?

static void Main(string[] args) {
    Stopwatch sw = new Stopwatch(); sw.Start(); 
    for (int i = 0; i < 10000000; i++) {
        double z = Math.Pow(i,2);
    }
    Console.WriteLine(sw.ElapsedMilliseconds);
    sw = Stopwatch.StartNew();
    for (int i = 0; i < 10000000; i++) {
        Noop();
    }
    Console.WriteLine(sw.ElapsedMilliseconds);
}

static void Noop() { }

      

The first cycle takes 1600 - 1700 milliseconds and the second cycle takes 3100 - 3200 milliseconds on my system (Celeron D 2.53 Ghz 512 MB RAM Windows XP SP3.NET 3.5). This is a command line project. I got similar results with VB.Net.

EDIT: I think I found the answer.

0


source to share


3 answers


1723 13 Is this more like what you would expect?

Yes, this is what I would expect. I am not working in debug mode (debug mode gives me 1900/3200). Running it with an open or closed IDE I get the same results. Using csc or using ngen install does not change the result. I think I have to reinstall the .net framework.

EDIT:



Thanks for answers.
Reinstalling the framework didn't fix the problem. After testing with a virtual machine, I got results that made sense. I finally found the problem:
A while ago I installed a profiler called ProfileSharp. I don't know what (or why) the profiler was doing without my permission while it was closed, but since removing it, I get 6ms for the second loop. I still need to reinstall it and run some tests to see if this is the culprit.

EDIT 2:

The profiler has an option "Automatically attach and start profiling new processes" which it enabled by default. When the profiler closes, it should stop profiling and does indeed do so in my tests with the VM, but for some reason when it didn't, and started profiling every .net process in the background (System, ASP and Microsoft namespaces are excluded by default, so it didn't have much impact on Math.Pow).

+4


source


Chances are you:

  • Built-in debug mode
  • Ran in the debugger.

My results:



c:\Users\Jon\Test>csc /o+ /debug- Test.cs
Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.1
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.


c:\Users\Jon\Test>test
1723
13

      

Is this more like what you would expect? Even with /debug+ /o-

I get much better results than your original ones when you run from the command line, which suggests that you were working in the debugger. Not a good idea for performance tests :)

+5


source


Regardless of the reason, it has nothing to do with method calls. On the one hand, such method calls are efficient. For another, your first example also calls a method. Third, the call to the second method will be inline, and since nothing happens, the second loop will be effectively completely empty. The compiler can even optimize the entire loop.

Finally, I cannot reproduce your result in either debug or release mode.

+1


source







All Articles