C#(CS2013)の配列に関する質問です。
ボールドテキスト****ボールドテキスト下記のコードは、ArrayByteA/Bをそれぞれ、staic領域およびヒープ領域に
とってその実行時間を測定するものです。
staic領域の実行時間:80ms
ヒープ領域の実行時間:72ms
Q1) staic領域の実行時間がヒープ領域の実行時間に比べて大きくなる原因は?
Q2) 上記の何れの場合も、ガーベージコレクションの対象になりますか?
Q3) 配列にアクセス中にも、ガーベージコレクションを行いますか?
==============
using System.Diagnostics;
using System.Threading;
namespace TTT_Array_stopWatch {
public partial class Form1:Form {
public static int length=10000000;
public byte[] ArrayByteA=new byte[length];
public Form1() {
InitializeComponent();
this.timer1 = new System.Windows.Forms.Timer();
this.timer1.Enabled = false;
this.timer1.Interval = 1;
this.timer1.Tick += new System.EventHandler(this.button3_Click);
}
private System.Windows.Forms.Timer timer1;
static int cnt=0;
static bool sw_stop=true;
private void button1_Click(object sender,EventArgs e) {
Stopwatch sw = new Stopwatch();
sw.Start();
for(int i=0;i<length;i++) {
ArrayByteA[i]=(byte)i;
}
sw.Stop();
String str;
int wk=(int)(double)(sw.ElapsedTicks/(double)Stopwatch.Frequency*1e3);
if(wk>90) {
str=string.Format("Timer1: time= {0}",wk);
listBox1.Items.Add(str);
sw_stop=true;
}
}
private void button2_Click(object sender,EventArgs e) {
byte[] ArrayByteB=new byte[10000000];
Stopwatch sw = new Stopwatch();
sw.Start();
for(int i=0;i<length;i++) {
ArrayByteB[i]=(byte)i;
}
sw.Stop();
String str;
int wk=(int)(double)(sw.ElapsedTicks/(double)Stopwatch.Frequency*1e3);
if(wk>80) {
str=string.Format("Timer2: time= {0}",wk);
listBox1.Items.Add(str);
sw_stop=true;
}
}
private void button3_Click(object sender,EventArgs e) {
timer1.Stop();
if(sw_stop==true) {
return;
}
if(++cnt%100==0) {
listBox1.Items.Clear();
listBox1.Items.Add(cnt);
}
button1_Click(sender,e);
button2_Click(sender,e);
timer1.Start();
}
private void button4_Click(object sender,EventArgs e) {
timer1.Start();
sw_stop=false;
}
}
}
以上、宜しくお願いします。
回答1件
あなたの回答
tips
プレビュー