colinfang
colinfang

Reputation: 21767

I am expecting myApp.exe runs faster than myApp.vshost.exe, but

from vshost (run in VS release)

 array pure 00:00:02.9634819
 1200000000
 Basic: 00:00:04.1682663

from standalone program (compiled release)

 array pure 00:00:09.1783278 // slower, why?
 1200000000
 Basic: 00:00:00.5985118  // faster, as expected

So it seems that running from VS sometimes speed up the programs?

My test code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace vsHostTest
{
    class Program
    {
        static long Five(int s0, int s1, int s2, int s3, int s4)
        {
            return s4 + 100 * s3 + 10000 * s2 + 1000000 * s1 + 100000000 * s0;
        }

        static void Main(string[] args)
        {
            Stopwatch watch = new Stopwatch();
            long testSize = 10000000;
            int[] myarray = new int[] { 1, 2, 3 };
            watch.Start();
            for (int j = 0; j < testSize; j++)
            {

                bool i = myarray.Contains(2);

            }
            watch.Stop();
            Console.WriteLine("array pure {0}", watch.Elapsed);

            testSize = 200000000;
            long checksum = 0;
            watch.Restart();
            for (long i = 0; i < testSize; i++)
            {
                long ret = Five(1, 2, 3, 4, 5);
                checksum += ret % 9;
            }
            watch.Stop();
            Console.WriteLine(checksum);
            Console.WriteLine("Basic: {0}", watch.Elapsed);
            Console.ReadKey();
        }
    }
}

Upvotes: 2

Views: 205

Answers (1)

Christopher Currens
Christopher Currens

Reputation: 30735

I ran each one four times, but not including the first result for each average.

vshost:

array pure: 6.83 Basic: 3.62

console:

array pure: 6.64 Basic: 1.57

I should add that all times were slower in vshost than they were in console. I'm not sure why you're getting the results you are, but vshost attaches a debugger to the process whereas running it via console does not. The console version will always be faster because of that.

Furthermore, while benchmarking .net applications, running the test once is not enough to get accurate measurements. You should always run the test multiple numbers of times, throwing out either the first (if you want don't want to compare cold runs, as .net caches a lot) or the most outlying measurement.

Also, and I feel stupid for asking this, are you sure you ran the release version when you were running via console? I'm sure you did, but I always ask, since I sometimes make silly mistakes like that.

Upvotes: 3

Related Questions