How many nanoseconds does a call to GetProperties() take?

ぐ巨炮叔叔 提交于 2020-01-15 11:25:09

问题


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:


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



    来源:https://stackoverflow.com/questions/9878960/how-many-nanoseconds-does-a-call-to-getproperties-take

  • 易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!