该程序中的秒表 [英] stopwatch in this program

查看:118
本文介绍了该程序中的秒表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是C#程序:
http://www.4shared.com/file/xAv2qAbB/SortComparison.html [ ^ ]
它具有计时器,该计时器在排序结束时显示排序时间,但是我需要此程序的秒表来显示从排序开始到结束的时间
是否需要另一个线程?
如果可以的话,用该定时器编辑我的代码

注意:我需要像秒表一样向我展示时间
我需要计时器显示每毫秒的排序时间.

here is a C# Program:
http://www.4shared.com/file/xAv2qAbB/SortComparison.html[^]
It has timer that shows sorting time at the end of sorting,but I need a stopwatch for this program that shows time from when sorting starts until its end
Does it need another thread?
if it is possible edit my code with that timer

Note:I need to show me time each moments like Stopwatch
I need timer shows time of sorting every millisecond

推荐答案

如果您想返回秒表的经过时间,您可以这样操作

If you want to return the elapsed time of the stopwatch you could do it like this

public IList BubbleSort(IList arrayToSort, out long elapsedTime)
        { 
            Stopwatch stopWatch = new Stopwatch(); 
            stopWatch.Start(); 
            int n = arrayToSort.Count - 1; 
            for (int i = 0; i < n; i++) 
            { 
                for (int j = n; j > i; j--)
                { 
                    if (((IComparable)arrayToSort[j - 1]).CompareTo(arrayToSort[j]) > 0)
                    { 
                        object temp = arrayToSort[j - 1]; 
                        arrayToSort[j - 1] = arrayToSort[j];
                        arrayToSort[j] = temp;
                        RedrawItem(j);
                        RedrawItem(j - 1); 
                        RefreshPanel(pnlSamples); 
                    } 
                    Thread.Sleep(speed);
                } 
            }
            stopWatch.Stop();
            elapsedTime = stopWatch.ElapsedMilliseconds;
            return arrayToSort; 
        }



那么你会这样称呼



then you would call it something like this

long methodTime;
            IList myList = BubbleSort(listToSort, out methodTime);



希望这对您有帮助



Hope this helps


好.
即使看起来很明显,也请尝试以下操作:

1.在最小范围内实例化System.Diagnostics.Stopwatch,涵盖排序算法的开始和结束.
2.在排序开始时启动秒表.
3.停止秒表排序.
4.获取排序时间.

如需更精确的帮助,请告诉我们确切的问题出在哪里.

提示:有些人(包括我在内)不喜欢从外部来源下载代码,最好使用改善问题"按钮张贴一个用"pre"标签括起来的相关代码段.
Fine.
Even though it seems obvious, try this:

1. Instantiate a System.Diagnostics.Stopwatch in the smallest possible scope covering start and ending of sorting algorithm.
2. Start stopwatch on start of sort.
3. Stop stopwatch on sort finish.
4. get sorting time.

For more precise help, tell us where exactly a problem arises.

Tip: Some peope (including me) don''t like downloading code from external sources, better post a relevant snippet enclosed by "pre" tags using the "improve question" button.


这篇关于该程序中的秒表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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