测试你的代码的速度?

发布于 2024-07-05 16:58:40 字数 147 浏览 6 评论 0原文

我完全是个新手,但我正在编写一个在 C# 中处理字符串的小程序,我注意到如果我做一些不同的事情,代码的执行速度会显着加快。

所以这让我想知道,如何计算代码的执行速度? 有(免费)实用程序吗? 您是否采用 System.Timer 的老式方法并自己完成?

I'm a total newbie, but I was writing a little program that worked on strings in C# and I noticed that if I did a few things differently, the code executed significantly faster.

So it had me wondering, how do you go about clocking your code's execution speed? Are there any (free)utilities? Do you go about it the old-fashioned way with a System.Timer and do it yourself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

朮生 2024-07-12 16:58:40

RedGate 的 ANTS Profiler 是一个非常好的性能分析器。 JetBrains 的 dotTrace Profiler 也很棒。 这些工具将允许您查看可以深入到每条线的性能指标。

ANTS Profiler 的屏幕截图:
ANTS http://www.red-gate.com/products/ ants_profiler/images/app/timeline_calltree3.gif

如果您想确保特定方法在单元测试期间保持在特定性能阈值内,我将使用 Stopwatch 用于在循环中监视某个方法的执行时间或多次计算平均值,然后 < code>Assert 针对结果。

ANTS Profiler from RedGate is a really nice performance profiler. dotTrace Profiler from JetBrains is also great. These tools will allow you to see performance metrics that can be drilled down the each individual line.

Scree shot of ANTS Profiler:
ANTS http://www.red-gate.com/products/ants_profiler/images/app/timeline_calltree3.gif

If you want to ensure that a specific method stays within a specific performance threshold during unit testing, I would use the Stopwatch class to monitor the execution time of a method one ore many times in a loop and calculate the average and then Assert against the result.

你丑哭了我 2024-07-12 16:58:40

只是提醒一下 - 确保在 Relase 中编译,而不是在 Debug 中编译! (我见过经验丰富的开发人员犯过这个错误 - 它很容易被忘记)。

Just a reminder - make sure to compile in Relase, not Debug! (I've seen this mistake made by seasoned developers - it's easy to forget).

朮生 2024-07-12 16:58:40

您所描述的是“性能调整”。 当我们谈论性能调优时,有两个角度。 (a) 响应时间 - 执行特定请求/程序需要多长时间。 (b) 吞吐量 - 一秒钟可以执行多少个请求。 当我们通常“优化”时 - 当我们消除不必要的处理时,响应时间和吞吐量都会提高。 但是,如果代码中有等待事件(例如 Thread.sleep()、I/O 等待等),您的响应时间会受到影响,但吞吐量不会受到影响。 通过采用并行处理(产生多个线程),我们可以提高响应时间,但吞吐量不会提高。 通常,对于服务器端应用程序,响应时间和吞吐量都很重要。 对于桌面应用程序(如 IDE),吞吐量并不重要,只有响应时间很重要。

您可以通过“性能测试”来衡量响应时间 - 您只需记下所有关键事务的响应时间。 您可以通过“负载测试”来测量吞吐量 - 您需要从足够多的线程/客户端连续泵送请求,以使服务器计算机的 CPU 使用率为 80-90%。 当我们发送请求时,我们需要维持不同交易之间的比率(称为交易组合) - 例如:在预订系统中,每 100 次搜索将有 10 次预订。 每 10 笔预订将会有 1 次取消,等等。

在识别出需要调整响应时间(性能测试)的交易后,您可以使用分析器来识别热点。
您可以通过比较该事务的响应时间 * 分数来识别吞吐量的热点。 假设在搜索、预订、取消场景中,比例为89:10:1。
响应时间为0.1秒、10秒和15秒。
搜索负载 - 0.1 * .89 = 0.089
预订负载 - 10 * .1 = 1
取消负载= 15 * .01= 0.15
这里调整预订将对吞吐量产生最大的影响。
您还可以通过重复获取线程转储(在基于 Java 的应用程序的情况下)来识别吞吐量的热点。

What are you describing is 'Performance Tuning'. When we talk about performance tuning there are two angle to it. (a) Response time - how long it take to execute a particular request/program. (b) Throughput - How many requests it can execute in a second. When we typically 'optimize' - when we eliminate unnecessary processing both response time as well as throughput improves. However if you have wait events in you code (like Thread.sleep(), I/O wait etc) your response time is affected however throughput is not affected. By adopting parallel processing (spawning multiple threads) we can improve response time but throughput will not be improved. Typically for server side application both response time and throughput are important. For desktop applications (like IDE) throughput is not important only response time is important.

You can measure response time by 'Performance Testing' - you just note down the response time for all key transactions. You can measure the throughput by 'Load Testing' - You need to pump requests continuously from sufficiently large number of threads/clients such that the CPU usage of server machine is 80-90%. When we pump request we need to maintain the ratio between different transactions (called transaction mix) - for eg: in a reservation system there will be 10 booking for every 100 search. there will be one cancellation for every 10 booking etc.

After identifying the transactions require tuning for response time (performance testing) you can identify the hot spots by using a profiler.
You can identify the hot spots for throughput by comparing the response time * fraction of that transaction. Assume in search, booking, cancellation scenario, ratio is 89:10:1.
Response time are 0.1 sec, 10 sec and 15 sec.
load for search - 0.1 * .89 = 0.089
load for booking- 10 * .1 = 1
load for cancell= 15 * .01= 0.15
Here tuning booking will yield maximum impact on throughput.
You can also identify hot spots for throughput by taking thread dumps (in the case of java based applications) repeatedly.

迷爱 2024-07-12 16:58:40

您所描述的称为性能分析。 您可以使用许多程序来执行此操作,例如 Jetbrains profilerAnts profiler,尽管大多数会在测量其性能的过程中减慢您的应用程序的速度。

要手动进行您自己的性能分析,您可以使用 System. Diagnostics.Stopwatch 和一个简单的 Console.WriteLine,就像您所描述的那样。

另请记住,C# JIT 编译器会根据调用的类型和频率来优化代码,因此请尝试不同大小的循环和递归调用等方法,以了解哪种方法最有效。

What you are describing is known as performance profiling. There are many programs you can get to do this such as Jetbrains profiler or Ants profiler, although most will slow down your application whilst in the process of measuring its performance.

To hand-roll your own performance profiling, you can use System.Diagnostics.Stopwatch and a simple Console.WriteLine, like you described.

Also keep in mind that the C# JIT compiler optimizes code depending on the type and frequency it is called, so play around with loops of differing sizes and methods such as recursive calls to get a feel of what works best.

琉璃梦幻 2024-07-12 16:58:40

使用分析器。

如果您只需要为一种特定方法计时,Stopwatch 类可能是一个不错的选择。

Use a profiler.

If you need to time one specific method only, the Stopwatch class might be a good choice.

っ〆星空下的拥抱 2024-07-12 16:58:40

我做以下事情:
1)我使用刻度(例如在VB.Net Now.ticks中)来测量当前时间。 我从完成的刻度值中减去起始刻度,然后除以 TimeSpan.TicksPerSecond 以获得花费的秒数。
2)我避免UI操作(如console.writeline)。
3)我通过大量循环(例如 100,000 次迭代)运行代码,以尽可能地分解使用情况/操作系统变量。

I do the following things:
1) I use ticks (e.g. in VB.Net Now.ticks) for measuring the current time. I subtract the starting ticks from the finished ticks value and divide by TimeSpan.TicksPerSecond to get how many seconds it took.
2) I avoid UI operations (like console.writeline).
3) I run the code over a substantial loop (like 100,000 iterations) to factor out usage / OS variables as best as I can.

幼儿园老大 2024-07-12 16:58:40

您可以使用 StopWatch 类来计时方法。 请记住,由于必须抖动代码,第一次通常会很慢。

You can use the StopWatch class to time methods. Remember the first time is often slow due to code having to be jitted.

壹場煙雨 2024-07-12 16:58:40

有一个本机 .NET 选项(面向软件开发人员的团队版)可以满足某些性能分析需求。 从 2005 .NET IDE 菜单中,选择“工具”->“性能工具”->“性能向导”...

[GSS 可能是正确的,您必须拥有团队版]

There is a native .NET option (Team Edition for Software Developers) that might address some performance analysis needs. From the 2005 .NET IDE menu, select Tools->Performance Tools->Performance Wizard...

[GSS is probably correct that you must have Team Edition]

嘦怹 2024-07-12 16:58:40

这是测试代码速度的简单示例。 我希望我对你有帮助

class Program {
    static void Main(string[] args) {
        const int steps = 10000;
        Stopwatch sw = new Stopwatch();

        ArrayList list1 = new ArrayList();
        sw.Start();
        for(int i = 0; i < steps; i++) {
            list1.Add(i);
        }
        sw.Stop();
        Console.WriteLine("ArrayList:\tMilliseconds = {0},\tTicks = {1}", sw.ElapsedMilliseconds, sw.ElapsedTicks);

        MyList list2 = new MyList();
        sw.Start();
        for(int i = 0; i < steps; i++) {
            list2.Add(i);
        }
        sw.Stop();
        Console.WriteLine("MyList:  \tMilliseconds = {0},\tTicks = {1}", sw.ElapsedMilliseconds, sw.ElapsedTicks);

This is simple example for testing code speed. I hope I helped you

class Program {
    static void Main(string[] args) {
        const int steps = 10000;
        Stopwatch sw = new Stopwatch();

        ArrayList list1 = new ArrayList();
        sw.Start();
        for(int i = 0; i < steps; i++) {
            list1.Add(i);
        }
        sw.Stop();
        Console.WriteLine("ArrayList:\tMilliseconds = {0},\tTicks = {1}", sw.ElapsedMilliseconds, sw.ElapsedTicks);

        MyList list2 = new MyList();
        sw.Start();
        for(int i = 0; i < steps; i++) {
            list2.Add(i);
        }
        sw.Stop();
        Console.WriteLine("MyList:  \tMilliseconds = {0},\tTicks = {1}", sw.ElapsedMilliseconds, sw.ElapsedTicks);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文