Are methods calls really so slow or is there something wrong in 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 loop takes 1600 - 1700 milliseconds, while the second loop takes 3100 - 3200 milliseconds in my system (Celeron D 2.53 Ghz 512 MB RAM Windows XP SP3 .NET 3.5). It's a commandline project. I got similar results with VB.Net .
EDIT: I think I found the answer.