在运行时如何在Windows 7中比较两个C#程序

how to compare two c# program in windows 7 in case of running time [closed]
2021-06-10
  •  译文(汉语)
  •  原文(英语)

我用onc#语言编写了两个程序.VS2010windows 7

我想根据运行时间对它们进行比较,换句话说,我想知道哪一个更快.

不幸的是,它们的运行时间以毫秒为单位.问题是当我看到它们中的一个较快时,分别运行它们时,但是接下来当我再次运行它们时,我看到另一个结果,我的意思是将一个更改更改为较慢.

Windows中是否有任何东西可以带来两个完全相等的环境以比较时间?

他们两个的输入文件是相同的,并且当我使用更大的输入文件时,时间在数秒之前,即使问题仍然存在

速聊1:
您的问题很难理解,您能否解释更多您想做的事情?另外,如果运行时间如此之近,您需要一个特殊的环境来对其进行测试,那么使用哪一个真的重要吗?
速聊2:
是的,先生,我是为大学研究项目所做的.您想知道的主题是数据包分类.我们增强了先前的算法.通过实现新结构的方式,我们只想现在哪种更快.对不起,您是高手,根据您的堆栈溢出分数,您的知识比我还多
速聊3:
我写了两个程序后解析器崩溃了
解决过程1

任何"测量都太快"的性能分析问题都可以通过重复执行来解决.在执行一千或一百万次的循环中运行测试代码.如果要测量单个cpu指令,则为十亿.

但这并不意味着你的测量是代表它要运行时,你只执行一次的方式.现代机器在做以前从未完成过的工作时非常糟糕,缓存是最重要的.但是您将获得代码摊销执行时间的度量.当您的代码需要处理真实数据集时,这一点才真正重要.

解决过程2

确保每次使用的环境完全相同的唯一方法是创建测试环境的映像并将其作为虚拟机运行.然后,您可以为每个新测试重新加载系统.

但是,可以以毫秒为单位测量运行时间,您只需要使用Stopwatch类即可.

Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// run your test here...
stopWatch.Stop();

TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
Console.WriteLine("RunTime " + elapsedTime);

另外,您也可以多次运行测试,每次都会平均出任何细微的差异.

速聊1:
我正是写这段代码!!!但是我有问题

I wrote two program with c# language in VS2010 on windows 7.

I want to compare them based on the running time, in other words I want to know which one is faster.

Unfortunately their running time duration are in millisecond scale .The problem is when I run them separately once I saw one of them is faster but next when I again repeat running them I see another result I mean faster one change to slower one.

Is there anything in windows which brings two totally equal environment in order to compare time?

Input file for both of them is same and when I use bigger input file time precedes to seconds but even though the problem still remains

Talk1:
Your question is very hard to understand, can you explain more what you are trying to do? Also if the running times are so close you need a special environment to test it out, does it really matter which one you use?
Talk2:
yes sir, I did that for university research project.As you like to know the topic is packet classification. We enhance prior algorithm .By the way we implemented new structure and we just want to now which one is faster. Excuse me you are master and you have more knowledge than me based your stack overflow score
Talk3:
my parser crashed after "I wrote two program"
Solutions1

Any "it's too fast to measure" profiling problem can be solved by doing it repeatedly. Run your test code in a loop that executes it a thousand or a million times. A billion if you are measuring individual cpu instructions.

That does not mean your measurement is representative for the way it is going to run when you only execute it once. Modern machines are very lousy at doing a job they've never done before, caches are King. But you'll get a measurement of the amortized execution time of the code. The one that really matters when your code needs to deal with real data sets.

Solutions2

The only way to ensure your environment is exactly the same for every time would be to create an image of your test environment and run it as a virtual machine. You can then reload the system with each new test.

However it is possible to measure the runtime in milliseconds, you just need to use the Stopwatch class.

Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// run your test here...
stopWatch.Stop();

TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
Console.WriteLine("RunTime " + elapsedTime);

Alternatively you can just run your tests multiple times and time each and any slight discrepancies would be averaged out.

Talk1:
I exactly write this code!!! But I have the problem
转载于:https://stackoverflow.com/questions/19082204/how-to-compare-two-c-sharp-program-in-windows-7-in-case-of-running-time

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入