1

In relation to: Big-O of .GetProperties()

How many nanoseconds does the .GetProperties() method take in C#?

EDIT

Tested:

On simple dev computer (nothing fancy), item has 8 properties, and no inheritance:

        stopwatch.Start();
        for (int i = 0; i < 10000; i++)
        {
            PropertyInfo[] properties = item.GetType().GetProperties();
        }
        stopwatch.Stop();

Results:

  • Total Time in Nanoseconds: 16,569.8
  • Total Time in Milliseconds: 16.5698
  • Average Time Per .GetProperties() Call: 1.65 ns (This is an assumption, not sure if the results are being cached)

    Moreover

    When ran a second time with an extra nested foreach loop this only took a total of 6 milliseconds. Here is the added code (inside the for loop):

                foreach (var prop in properties)
                {
                    var x = prop;
                }
    
  • Community
    • 1
    • 1
    Travis J
    • 81,153
    • 41
    • 202
    • 273
    • What it takes will vary on any number of parameters. It's impossible to state with absolute certainty what the execution time of it, or really any, piece of code will be. – JaredPar Mar 26 '12 at 20:06
    • @JaredPar - I was having trouble finding what those parameters are, or any benchmarks. Do you know of where I could look? – Travis J Mar 26 '12 at 20:08
    • http://www.smelser.net/blog/post/2011/02/13/Knowing-what-things-cost.aspx – Austin Salonen Mar 26 '12 at 20:12
    • Note that the Reflection tag is misspelled. How come I cannot retag this post but I can do so with others? – Alejandro B. Mar 26 '12 at 20:12
    • @MattBurland - See edit for a test on some mediocre hardware. – Travis J Mar 26 '12 at 20:34
    • Well, you've got a first-order answer. But do realize it's very artificial, the actual timing in a program could be different. But do you really have a situation where this could be the bottleneck? – H H Mar 26 '12 at 20:41
    • @HenkHolterman - Nope, but I hear so many people say `"Reflection is slow"`, so I am trying to get some feel of how much protection I should put in code to avoid having to use it. I use it in one case to exclude flagged records from a database set - it runs very fast and I do not notice any performance hit. I can filter the results in various ways which requires extra coding and logic, and have done that as well but I am just trying to get a feel for why people seem to think reflection is so slow. – Travis J Mar 26 '12 at 20:45
    • Reflection might be a little slow but any Database I/O will be many orders of magnitude slower. – H H Mar 26 '12 at 20:48
    • Your nested foreach loop does make any significant change. It's only looping through a pre-filled array. The fact that you got 6ms on the second run is pretty random - the speed varies a little. Run it a few more times and until 100000, and you will get the bigger picture. – SimpleVar Mar 27 '12 at 04:20
    • @TravisJ: If you are using it, and you are happy with the performance, then why worry about it? – Matt Burland Mar 27 '12 at 13:09

    1 Answers1

    3

    Given your complete application code, a complete description of your hardware, your registry or system configuration, your operating system version, and a list of all running software and their activities, it should be possible to come up with a reasonable guess.

    What I am trying to say is that it depends. On almost everything. The only way to find out is to time it, preferably with a Stopwatch and over many iterations. The exact result varies from time to time, and is mostly dependent on the class that you are examining, the amount of CPU power available, and luck (aka processor scheduling).

    Kendall Frey
    • 43,130
    • 20
    • 110
    • 148
    • See edit for stopwatch results. I barely had time to blink when running the test over a loop of 10,000. – Travis J Mar 26 '12 at 20:31