none
I thought the Stack and Reflection was supposed to be slow? RRS feed

  • Question

  • Hi,

    I have allways been under the impression that inspecting the Stack and using reflection should be avoided in code that must perform well. However now I have a need that might call for Stack usage. I must fetch a Frame and get the methods declaration type name like so (ignoring possible null references):

    var name = new StackFrame(2, false).GetMethod().DeclaringType.FullName;

    So just out of curiosity I create som tests using a StopWatch. And it turns out that the above code runs just as fast as any other object instantiation would. I time it to around 0.02 ms / avg.

    I know there are lots of different usage of Stack and reflection that probably is slow, but is it safe to conclude that using the Stack as above is actually allright in high performance code?

    --
    Werner

    Wednesday, March 16, 2011 12:31 PM

Answers

  • Hi Werner,

     

    Welcome to the MSDN forum!

    Yes, you can do in this way.

    It is the same as you just do a reflection to an object that stay in the position corresponds to specified frame above the current stack frame.

    e.g. var name =object.GetMethod().DeclaringType.FullName;

     

    Much further information about StackFrame


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Werner Clausen Wednesday, March 23, 2011 9:32 AM
    Friday, March 18, 2011 8:11 AM
  • Hi Werner,

    Do you think the time it would take would increase with the depth of the call stack. 

    I ran it just there in the static main class and it was fast. Wondering if it would be as fast called from a deep within an algorithm (or like a recursive method).

     

     


    "The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination." - Fred Brooks
    • Marked as answer by Werner Clausen Wednesday, March 23, 2011 9:32 AM
    Friday, March 18, 2011 8:38 AM
  • A guess: stackwalking and (maybe) reflection associated with that may be fast because the basic methods underneath may be used by garbage collection.
    Phil Wilson
    • Marked as answer by Werner Clausen Wednesday, March 23, 2011 9:32 AM
    Friday, March 18, 2011 9:44 PM

All replies

  • Hi Werner,

     

    Welcome to the MSDN forum!

    Yes, you can do in this way.

    It is the same as you just do a reflection to an object that stay in the position corresponds to specified frame above the current stack frame.

    e.g. var name =object.GetMethod().DeclaringType.FullName;

     

    Much further information about StackFrame


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Werner Clausen Wednesday, March 23, 2011 9:32 AM
    Friday, March 18, 2011 8:11 AM
  • Hi Werner,

    Do you think the time it would take would increase with the depth of the call stack. 

    I ran it just there in the static main class and it was fast. Wondering if it would be as fast called from a deep within an algorithm (or like a recursive method).

     

     


    "The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination." - Fred Brooks
    • Marked as answer by Werner Clausen Wednesday, March 23, 2011 9:32 AM
    Friday, March 18, 2011 8:38 AM
  • A guess: stackwalking and (maybe) reflection associated with that may be fast because the basic methods underneath may be used by garbage collection.
    Phil Wilson
    • Marked as answer by Werner Clausen Wednesday, March 23, 2011 9:32 AM
    Friday, March 18, 2011 9:44 PM
  • Thanks all. Helpful answers which made me test some more. Seems it really doesnt matter how and where this code is called (recursively, stack depth etc). The performance is always around 0.02 ms. I have decided to include similar code into some common libraries we have - which are all monitored and instrumented. I'll see how it performs outside my little test area.

    Thanks for your answers - don't know which to mark as "answer" :) ??

    --
    Werner


    Monday, March 21, 2011 7:52 AM
  • Hi Werner,

     

    You can choose one or more relies to make as answers if they are helpful for you.

     

    Have a nice day!


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 23, 2011 2:30 AM
  • Hi Werner,

     

    You can choose one or more relies to make as answers if they are helpful for you.

     

    Have a nice day!


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Yes I could :) - didn't know that or haven't used it consciously until now that is...thanks..

     


    Wednesday, March 23, 2011 9:34 AM