我想知道输入大量数字时 'arraylist' 的性能。
'arraylist' 添加效果很好。但是删除'arraylist'需要很多时间。
例如,当我输入数字“10000000”时,添加速度很快。移除大约需要 15 分钟。
如何解决?
public partial class Form1 : Form
{
Stopwatch stopwatch = new Stopwatch();
ArrayList arrayList = new ArrayList();
Hashtable hashtable = new Hashtable();
HashSet<string> hashSet = new HashSet<string>();
SortedList sortedList = new SortedList();
Stack stack = new Stack();
Queue queue = new Queue();
static List<string > list = new List<string>() ;
static Random rand = new Random(50000);
static long count = 0;
static string inputCount = "";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
inputCount = countTextbox.Text;
count = Convert.ToInt64(inputCount);
}
private void arrayListBtn_Click(object sender, EventArgs e)
{
for (int i = 0; i < count; i++)
{
list.Add($"T_{i.ToString("000000000")}");
}
Console.WriteLine(list.Count);
stopwatch.Reset();
stopwatch.Start();
for (int i = 0; i < list.Count; i++)
{
}
foreach (var item in list.ToList())
{
arrayList.Add(rand);
}
Console.WriteLine(arrayList.Count);
stopwatch.Stop();
arrayListAddTextBox.Text = stopwatch.Elapsed.ToString();
stopwatch.Reset();
stopwatch.Start();
foreach (var item in list.ToList())
{
arrayList.Remove(item);
}
Console.WriteLine(arrayList.Count);
stopwatch.Stop();
arrayListRemoveTextBox.Text = stopwatch.Elapsed.ToString();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句