什么是衡量花费$多久C $ C到执行的最佳方法? [英] What is the best way to measure how long code takes to execute?

查看:95
本文介绍了什么是衡量花费$多久C $ C到执行的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图确定哪种方法去除一个字符串在最快

我只是得到的开始结束时间和示区别。

但结果是如此的变化,例如如示出的方法相同的方法在下面可以从60毫秒到231毫秒。

什么是更好的方法来获得更精确​​的结果?

 使用系统;
System.Collections中使用;
使用System.Collections.Generic;命名空间TestRemoveFast
{
    类节目
    {
        静态无效的主要(字串[] args)
        {
            为(中间体J = 0; J&小于10; J ++)
            {
                字符串newone =;
                清单<串GT;测试=新名单<串GT;();
                的for(int i = 0; I< 100000;我++)
                {
                    tests.Add({http://company.com/Services/Types}ModifiedAt);
                }                日期时间开始= DateTime.Now;
                的foreach(在测试中测试VAR)
                {
                    // newone =((System.Xml.Linq.XName){http://company.com/Services/Types}ModifiedAt).LocalName;
                    newone =清洁(试验);
                }                Console.WriteLine(newone);
                日期时间结束= DateTime.Now;
                时间跨度持续时间=结束 - 启动;
                Console.WriteLine(duration.ToString());
            }            到Console.ReadLine();
        }        静态的字符串清洁(串线)
        {
            INT POS = line.LastIndexOf('}');
            如果(POS大于0)
                返回line.Substring(POS + 1,line.Length - POS - 1);
                //返回line.Substring(POS + 1);
            其他
                回线;
        }
    }
}


解决方案

您应该使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx\">System.Diagnostics.Stopwatch,你可能要考虑一个大样本。例如,重复此试验类似于10,000次并平均结果。如果你科学地想一想,这是有道理的。该样本越大,效果越好。你可以剔除掉大量的边缘情况下,这种方式确实看到了核心的性能是什么样的。

要考虑的另一件事是,JIT编译和对象创建绝对可以扭曲你的结果,所以请确保您启动并在适当的时候停止秒表,并在开始之前打电话给你要测试的方法,至少有一次你试验。尽量隔离只是你想从code其余尽可能多的测试尽可能的部分。

I'm trying to determine which approach to removing a string is the fastest.

I simply get the start and end time and show the difference.

But the results are so varied, e.g. as shown below the same method can take from 60 ms to 231 ms.

What is a better method to get more accurate results?

using System;
using System.Collections;
using System.Collections.Generic;

namespace TestRemoveFast
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int j = 0; j < 10; j++)
            {
                string newone = "";
                List<string> tests = new List<string>();
                for (int i = 0; i < 100000; i++)
                {
                    tests.Add("{http://company.com/Services/Types}ModifiedAt");
                }

                DateTime start = DateTime.Now;
                foreach (var test in tests)
                {
                    //newone = ((System.Xml.Linq.XName)"{http://company.com/Services/Types}ModifiedAt").LocalName;
                    newone = Clean(test);
                }

                Console.WriteLine(newone);
                DateTime end = DateTime.Now;
                TimeSpan duration = end - start;
                Console.WriteLine(duration.ToString());
            }

            Console.ReadLine();
        }

        static string Clean(string line)
        {
            int pos = line.LastIndexOf('}');
            if (pos > 0)
                return line.Substring(pos + 1, line.Length - pos - 1);
                //return line.Substring(pos + 1);
            else
                return line;
        }
    }
}

解决方案

You should use System.Diagnostics.Stopwatch, and you might want to consider a large sample. For example, repeat this test something like 10,000 times and average the results. If you think about it scientifically, it makes sense. The larger the sample, the better. You can weed out a lot of edge cases this way and really see what the core performance is like.

Another thing to consider is that JIT compilation and object creation can definitely skew your results, so make sure that you start and stop your Stopwatch at the appropriate times, and call the methods you want to test at least once before you begin your tests. Try to segregate just the parts you want to test from the rest of your code as much as possible.

这篇关于什么是衡量花费$多久C $ C到执行的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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