iT邦幫忙

0

C# 陣列問題

c#

各位好,最近開始學習C# 目前學到陣列

有一個作業想了好幾天想不太出來..

想請問各位大神能不能提點一下 感恩

這個作業是要寫 用Array.sort來測試 分別跑1000/2000/4000/8000 筆資料 所需要的時間(ms)

目前困難點是,不知道怎樣分開紀錄時間...

第一次發文如有冒犯請見諒XD

目前寫到這裡
https://ithelp.ithome.com.tw/upload/images/20210429/201370749m9KqfZDEv.png

執行後是這樣
https://ithelp.ithome.com.tw/upload/images/20210429/20137074hQ3qIvDIeU.png

謝謝各位!

1
japhenchen
iT邦大師 1 級 ‧ 2021-04-29 07:50:54

把Array.sort執行前的時間記下

DateTime dtBefore = DateTime.Now;
Array.Sort();
var TotalPeriod = (DateTime.Now - dtBefore).TotalSeconds;
MessageBox.Show($"總共用掉{TotalPeriod}秒");
1
海綿寶寶
iT邦大神 1 級 ‧ 2021-04-29 08:48:32

https://ithelp.ithome.com.tw/upload/images/20210429/200017872lBD9c7PFX.png

https://ithelp.ithome.com.tw/upload/images/20210429/20001787s2Kv3X3krQ.png

看更多先前的回應...收起先前的回應...

您好!
我使用您建議的方式 產生一些錯誤 再麻煩您給我指教謝謝!https://ithelp.ithome.com.tw/upload/images/20210430/201370747sp9goxAdl.png

 //Array.Sort 排序後
                s.Reset();
                s.Start();
                for (int i = 0; i < A.Length - 1; i++)
                {                   
                    /*Console.WriteLine("A[{0}]={1,4}", i, A[i]);*/                
                }
                s.Stop();
                float t1k, t2k, t4k, t8k;
                
                switch (test)
                {

                    case 1000:
                        t1k= 1000.0f * s.ElapsedTicks / Stopwatch.Frequency;
                        break;
                    case 2000:
                        t2k = 1000.0f * s.ElapsedTicks / Stopwatch.Frequency;
                        break;
                    case 4000:
                        t4k = 1000.0f * s.ElapsedTicks / Stopwatch.Frequency;
                        break;
                    case 8000:
                        t8k = 1000.0f * s.ElapsedTicks / Stopwatch.Frequency;
                        break;
                }              
                test = test * 2;
            } while (test < 8001);
            
            Console.WriteLine("  Size\t Array.Sort ");
            /*Console.WriteLine("{0} ticks", s.ElapsedTicks);*/
            /*Console.WriteLine(Stopwatch.Frequency);*/
            Console.Write("  1000\t     {0} \n  2000\t     {1}\n  4000\t   {2}\n  8000\t   {3}", t1k, t2k, t4k, t8k);

什麼錯誤?可否說明一下?

s.Reset();
s.Start();
for (int i = 0; i < A.Length - 1; i++)
   {                   
      /*Console.WriteLine("A[{0}]={1,4}", i, A[i]);*/                
   }
s.Stop();

你要算的時間
是這個什麼事都沒做的迴圈嗎?

抱歉!
我覺得自己表達的方式可能不太好

要輸出結果的時候 它顯示錯誤 t1k, t2k, t4k, t8k不存在目前內容中

Stopwatch s = new Stopwatch();
            //Array.Sort 排序前           
            int test = 10;

            do
            {
                int[] A = new int[test];
                for (int i = 0; i <= test - 1; i++)
                {
                    A[i] = i;
                }
                Random rng = new Random();
                for (int i = 0; i < A.Length - 1; i++)
                {
                    int j = rng.Next(A.Length);
                    int tmp = A[i];
                    A[i] = A[j];
                    A[j] = tmp;
                }
                for (int i = 0; i < A.Length; i++)
                {
                    Console.WriteLine("A[{0}]={1,4}", i, A[i]);
                }
                Array.Sort(A);

                //Array.Sort 排序後
                s.Reset();
                s.Start();
                for (int i = 0; i < A.Length; i++)
                {
                    Console.WriteLine("A[{0}]={1,4}", i, A[i]);
                }
                s.Stop();
                float t1k, t2k, t4k, t8k;

                switch (test)
                {

                    case 1000:
                        t1k = 1000.0f * s.ElapsedTicks / Stopwatch.Frequency;
                        break;
                    case 2000:
                        t2k = 1000.0f * s.ElapsedTicks / Stopwatch.Frequency;
                        break;
                    case 4000:
                        t4k = 1000.0f * s.ElapsedTicks / Stopwatch.Frequency;
                        break;
                    case 8000:
                        t8k = 1000.0f * s.ElapsedTicks / Stopwatch.Frequency;
                        break;
                }
                test = test * 2;
            } while (test < 8001);
            
            Console.WriteLine("  Size\t Array.Sort ");
            Console.WriteLine("{0} ticks", s.ElapsedTicks);
            Console.WriteLine(Stopwatch.Frequency);
            Console.Write("  1000\t     {0} \n  2000\t     {1}\n  4000\t   {2}\n  8000\t   {3}", t1k, t2k, t4k, t8k);

謝謝!

float t1k, t2k, t4k, t8k;
搬到int test = 1000;的下面試看看

還是沒辦法 我再研究看看怎樣改

我是指「接在 int test=1000;的下一列」,如下:

using System.IO;
using System.Diagnostics;
using System;

class Program
{
    static void Main()
    {
            Stopwatch s = new Stopwatch();
            //Array.Sort 排序前           
            int test = 1000;
            float t1k=0, t2k=0, t4k=0, t8k=0;

            do
            {
                Console.WriteLine(test);
                int[] A = new int[test];
                for (int i = 0; i <= test - 1; i++)
                {
                    A[i] = i;
                }
                // Random rng = new Random();
                // for (int i = 0; i < A.Length - 1; i++)
                // {
                //     int j = rng.Next(A.Length);
                //     int tmp = A[i];
                //     A[i] = A[j];
                //     A[j] = tmp;
                // }
                // for (int i = 0; i < A.Length; i++)
                // {
                //     Console.WriteLine("A[{0}]={1,4}", i, A[i]);
                // }
                
                s.Reset();
                s.Start();
                
                Array.Sort(A);
                
                s.Stop();

                //Array.Sort 排序後
                // s.Reset();
                // s.Start();
                // for (int i = 0; i < A.Length; i++)
                // {
                //     Console.WriteLine("A[{0}]={1,4}", i, A[i]);
                // }
                // s.Stop();

                switch (test)
                {
                    case 1000:
                        t1k = 1000.0f * s.ElapsedTicks / Stopwatch.Frequency;
                        break;
                    case 2000:
                        t2k = 1000.0f * s.ElapsedTicks / Stopwatch.Frequency;
                        break;
                    case 4000:
                        t4k = 1000.0f * s.ElapsedTicks / Stopwatch.Frequency;
                        break;
                    case 8000:
                        t8k = 1000.0f * s.ElapsedTicks / Stopwatch.Frequency;
                        break;
                }
                test = test * 2;
            } while (test < 8001);
            
            Console.WriteLine("  Size\t Array.Sort ");
            Console.WriteLine("{0} ticks", s.ElapsedTicks);
            Console.WriteLine(Stopwatch.Frequency);
            Console.Write("  1000\t     {0} \n  2000\t     {1}\n  4000\t   {2}\n  8000\t   {3}", t1k, t2k, t4k, t8k);
    }
}
0
rogeryao
iT邦大師 3 級 ‧ 2021-05-05 09:17:11

... 會錯意

我要發表回答

立即登入回答