How many nanoseconds does a call to GetProperties(

2019-08-05 14:42发布

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;
                }
    
  • 1条回答
    我命由我不由天
    2楼-- · 2019-08-05 15:17

    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).

    查看更多
    登录 后发表回答