调用 GetProperties() 需要多少纳秒? [英] How many nanoseconds does a call to GetProperties() take?

查看:35
本文介绍了调用 GetProperties() 需要多少纳秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于:.GetProperties() 的大O

.GetProperties() 方法在 C# 中需要多少纳秒?

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

编辑

已测试:

在简单的开发计算机上(没什么特别的),item 有 8 个属性,并且没有继承:

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();

结果:

  • 总时间(以纳秒为单位):16,569.8
  • 总时间(以毫秒为单位):16.5698
  • 每个 .GetProperties() 调用的平均时间:1.65 ns(这是一个假设,不确定结果是否被缓存)

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

    此外

    第二次使用额外的嵌套 foreach 循环运行时,总共只需要 6 毫秒.这是添加的代码(在 for 循环内):

    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;
                }
    

    推荐答案

    给定您完整的应用程序代码、硬件的完整描述、您的注册表或系统配置、您的操作系统版本以及所有正在运行的软件及其活动,应该可以做出合理的猜测.

    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.

    我想说的是这取决于.几乎所有的东西.找出答案的唯一方法是计时,最好使用 Stopwatch 并经过多次迭代.确切的结果不时变化,主要取决于您正在检查的课程、可用的 CPU 能力以及运气(也称为处理器调度).

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

    这篇关于调用 GetProperties() 需要多少纳秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆