日期:2014-05-18 浏览次数:21474 次
for (string s = Console.ReadLine(); s != ""; s = Console.ReadLine())
{
// ......
}
------解决方案--------------------
foreach 适合 对象遍历
for适合变量算法遍历
单纯外部 性能问题??
for(int i = 0; i <a.count; i++)
.count 需要计算
[i] 需要指向!
------解决方案--------------------
测一下不就知道了
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press enter to continue testing!");
do
{
For_Each.TestEfficiency();
Console.WriteLine("-------------------------------------------");
} while (Console.ReadKey().Key == ConsoleKey.Enter);
}
static void TestEfficiency()
{
const int COUNT = 100000000;
Stopwatch sw = new Stopwatch();
//改集合类型 ToList()
var list = Enumerable.Range(1, COUNT).ToArray();
int temp = 0;
sw.Start();
for (int i = 0; i < COUNT; ++i) temp = 0 - list[i];
sw.Stop();
Console.WriteLine("for circulate elapsed time:{0}", sw.ElapsedMilliseconds);
temp = 0;
sw.Restart();
foreach (var i in list) temp = 0 - i;
Console.WriteLine("foreach circulate elapsed time:{0}", sw.ElapsedMilliseconds);
}