- array[i, j] 與 array[j, i] 執行速度上的差別。
- 使用碼表:Stopwatch() 顯示執行時間。
##ReadMore##
Step1. 檔案 → 新增專案 → Windows Form 應用程式 → 拉物件 Form1.cs[設計] → 撰寫 Form1.cs
Step2. Windows Form 拉入需要工具:
- textBox × 1
- button × 1
Step3. Coding
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace Elapsed_Time
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Stopwatch stopWatch = new Stopwatch();
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
stopWatch.Reset();
stopWatch.Start();
int maxi = 5000;
int maxj = 5000;
int[,] array = new int[maxi, maxj];
for (int i = 0; i < maxi; i++)
{
for (int j = 0; j < maxj; j++)
{
/* 測試 Array [i, j] 與 [j, i] 執行速度差異(二選一來執行) */
/* [i, j] 會執行比較快 */
array[i, j] = i * j;
/* [j, i] 比較慢,較花時間 */
//array[j, i] = i * j;
}
}
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
textBox1.Text = ts.ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
沒有留言:
張貼留言