Click here to Skip to main content
15,887,861 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
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
Posted
Updated 1-Jun-11 16:36pm
v5

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

C#
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

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


Hope this helps
 
Share this answer
 
Comments
Member 7904482 1-Jun-11 8:18am    
I dont understand what it is:
long methodTime;
IList myList = BubbleSort(listToSort, out methodTime);
Wayne Gaylard 1-Jun-11 8:51am    
methodTime is a long (as in long integer) variable which will be used to store the elapsed time of the stopwatch. The second line just shows you how to call the method, and assign the result to an IList, which in this case I called myList just for an example.
Sergey Alexandrovich Kryukov 1-Jun-11 15:04pm    
Always use double type for elapsed time, such as TotalMilliseconds, TotalSeconds. Other than that, nice post. My 4.
--SA
Wayne Gaylard 2-Jun-11 2:38am    
Thanks, though the StopWatch.ElapsedMilliSeconds returns a long integer.
Sergey Alexandrovich Kryukov 2-Jun-11 2:46am    
Understand. The nature of Time make floating-point a better model then even long integer, especially for performance test.
--SA
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.
 
Share this answer
 
Comments
Member 7904482 1-Jun-11 6:07am    
I write code like this:
public IList BubbleSort(IList arrayToSort)
{
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);
}
}
return arrayToSort;
stopWatch.Stop();
}
but how to show the time
Sergey Alexandrovich Kryukov 1-Jun-11 15:05pm    
Why do you use Thread.Sleep?!! You time nothing in this case.
--SA
Member 7904482 1-Jun-11 6:11am    
could you change my code and show this
thanks
yesotaso 1-Jun-11 6:55am    
See Stopwatch Class there is example code there aswell.
Member 7904482 1-Jun-11 7:31am    
I see it, but I think it show the time at the end of sorting. I said that I need timer show me time each moments like Stopwatch

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900