62,046
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
const Int32 nummax = 1000*1000*1000;
//Demo.x = 123;将初始化代码添加到此处Test1与Test2执行效率相同
Test1(nummax);
Test2(nummax);
Console.Read();
}
static void Test1(int nummax)
{
Stopwatch sw = new Stopwatch();
sw.Start();
Demo.x = 123;//将初始化代码添加到此处执行效率并没有被优化
for (int i = 0; i < nummax; i++)
{
Demo.x = 123;
}
Console.WriteLine("Test1:{0} Test1",sw.Elapsed);
}
static void Test2(int nummax)
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < nummax; i++)
{
Demo.x = 123;
}
Console.WriteLine("Test2:{0} Test2", sw.Elapsed);
}
}
internal sealed class Demo
{
public static Int32 x=5;
static Demo()
{
x = 123;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace Demo
{
class Program
{
static int j;
static void Main(string[] args)
{
const Int32 nummax = 1000 * 1000 * 1000;
//Demo.x = 123;//将初始化代码添加到此处Test1与Test2执行效率相同
//j = new int();
Console.WriteLine("2");
Test1(nummax);
Console.WriteLine("3");
Test2(nummax);
Console.WriteLine("4");
Console.Read();
}
static void Test1(int nummax)
{
Stopwatch sw = new Stopwatch();
//Demo.x = 123;//将初始化代码添加到此处执行效率并没有被优化
j = new int();
sw.Start();
for (int i = 0; i < nummax; i++)
{
j = 123;
//Demo.x = 123;
}
Console.WriteLine("Test1:{0} Test1", sw.Elapsed);
}
static void Test2(int nummax)
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < nummax; i++)
{
j = 123;
//Demo.x = 123;
}
Console.WriteLine("Test2:{0} Test2", sw.Elapsed);
}
}
internal sealed class Demo
{
public static Int32 x = 5;
static Demo()
{
x = 123;
}
}
}