1

I created a code in order to calculate the time of running.

It's working. But I couldn't explain: What is the difference between variables in Heap and Stack?

I'm talking about "running time".

I created 3 variables in Heap: a, b, c.

And 3 variables in Stack: aa, bb, cc.

My code:

class Program
{
    private int a = 1;
    private int b = 2;
    private int c = 0;

    static void Main()
    {
        int aa = 1;
        int bb = 2;
        int cc = 0;

        var sw = new Stopwatch();
        var _sw = new Stopwatch();

        Program pro = new Program();

        for (int k = 0; k < 10; k++)
        {
            sw.Start();
            for (int i = 0; i < 500000000; i++)
            {
                pro.c += pro.a + pro.b;
            }
            sw.Stop();

            Console.WriteLine("Heap:");
            Console.WriteLine("TotalMiliseconds: {0}", sw.Elapsed.TotalMilliseconds.ToString());
            Console.WriteLine("__________________________________");
            sw.Reset();

            _sw.Start();
            for (int j = 0; j < 500000000; j++)
            {
                cc += aa + bb;
            }
            _sw.Stop();

            Console.WriteLine("Stack:");
            Console.WriteLine("TotalMiliseconds: {0}", _sw.Elapsed.TotalMilliseconds.ToString());
            Console.WriteLine("__________________________________");

            _sw.Reset();
        }
        Console.ReadKey();
    }

}

Here is my question: Can you tell me why the running time in Heap is always faster than Stack?

Thanks!

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • Related: [What and where are the stack and heap?](http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap) – Soner Gönül Sep 29 '14 at 06:21
  • 1
    According to your source, the heap is slower than the stack, which is in contrast of what kevin is asking. It doesn't provide any info about cases in which the heap is faster. – Allmighty Sep 29 '14 at 06:37
  • When I run your code the 'heap' loop always executes slower, by about 10%. – Rotem Sep 29 '14 at 07:20
  • Right? My result: the Heap loop is always faster than the Stack loop about 0.2 seconds. (Heap: 2.0 <=> Stack 2.2 - Heap: 2.1 <=> Stack: 2.4 ...) –  Sep 29 '14 at 07:27
  • Actually, I ran this, and the heap is always taking longer than the stack for me. I guess stakx is right and this is entirely runtime environment dependent. Oddly enough, when I reversed the order of the inner loops, the difference between the two was reduced. – o_weisman Sep 30 '14 at 06:38

1 Answers1

-3

To the best of my understanding, you are not comparing stack access to heap access, since both sets of variables are allocated on the stack as they are all value types. See here: http://msdn.microsoft.com/en-us/library/4d43ts61(v=vs.90).aspx

o_weisman
  • 804
  • 7
  • 19
  • 1
    The value type / reference type dichotomy doesn't really come into play here. The only relevant aspect of this is that the value-typed fields `a`, `b`, and `c` get "embedded" in their `Program` instances, i.e. are stored in-place instead of being located somewhere else and only referenced from inside a `Program` instance. Since the OP is `new`-ing a `Program` instance, one could surmise that it will land on "the heap". And one could surmise that local variables always land on "the stack". But that's just guessing. – stakx - no longer contributing Sep 30 '14 at 06:10
  • 1
    Whether objects are put on "the stack" or "the heap", and whether these constructs even exist in the first place, is mostly an implementation detail of and a choice made by the runtime environment (the CLR), not something that can be forced in, or known by looking at, source code. (That being said, the CLR admittedly does work according to fixed rules, so `new` instances probably really *do* end up on the heap, and local variables in registers or on the stack.) – stakx - no longer contributing Sep 30 '14 at 06:11
  • @stakx If you are correct, then you should probably give that as the answer instead of stating it in comments. – o_weisman Sep 30 '14 at 06:22
  • I'm not sure whether my comments would actually be a sufficient answer to the actual question. I just wanted them to explain why I downvoted your answer (sorry). Will think about it. – stakx - no longer contributing Sep 30 '14 at 16:35