0

## C# 陣列問題

### 3 個回答

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

``````DateTime dtBefore = DateTime.Now;
Array.Sort();
var TotalPeriod = (DateTime.Now - dtBefore).TotalSeconds;
MessageBox.Show(\$"總共用掉{TotalPeriod}秒");
``````
1

iT邦大神 1 級 ‧ 2021-04-29 08:48:32

`````` //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();
``````

``````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;`

``````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

... 會錯意