参考地址:1.http://www.c-sharpcorner.com/UploadFile/dacca2/5-tips-to-improve-performance-of-C-Sharp-code/
2.http://blog.csdn.net/xxdddail/article/details/11026289
1、数组添加性能>泛型List<T>
代码
staticvoid Main(string[] args)
{
List<int> li = new List<int>();
Stopwatch sw =new Stopwatch();
sw.Start();
for (int i = 0; i < 10000; i++)
{
li.Add(i);
}
sw.Stop();
Console.Write("Using Arraylist(Object)" + sw.ElapsedTicks + "\n");
sw.Reset();
sw.Start();
int[] a = new int[10000];
for (int i = 0; i < 10000; i++)
{
a[i] = i;
}
sw.Stop();
Console.Write("Using Value(Integer Array)" + sw.ElapsedTicks);
Console.ReadLine();
}
亲测结果
staticvoid Main(string[] args)
{
List<string> li = new List<string>();
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 10000; i++)
{
li.Add("123");
}
sw.Stop();
Console.Write("Using Arraylist(Object)" + sw.ElapsedTicks + "\n");
sw.Reset();
sw.Start();
string[] a = new string[10000];
for (int i = 0; i < 10000; i++)
{
a[i] = "123";
}
sw.Stop();
Console.Write("Using Value(Integer Array)" + sw.ElapsedTicks);
Console.ReadLine();
}
亲测结果
原因分析
List<T>会多次拆箱装箱(值类型和object之间转换),耗费大量的性能
2、List<T>性能 > Dictionary<T, T>性能
代码
static void Main(string[] args)
{
DicTest();
}
/// <summary>
/// dic测试
/// </summary>
private static void DicTest()
{
List<Int32> li = new List<Int32>(1000);
Dictionary<int, int> di = new Dictionary<int, int>(1000);
for (int i = 0; i < 1000; i++)
{
li.Add(i);
}
Stopwatch sw = new Stopwatch();
sw.Stop();
Console.WriteLine("From list:- " + sw.ElapsedTicks);
sw.Restart();
for (int i = 0; i < 1000; i++)
{
di.Add(i,i);
}
sw.Stop();
Console.WriteLine("From Dictionary:- " + sw.ElapsedTicks);
Console.ReadLine();
}
static void Main(string[] args)
{
DicTest();
}
/// <summary>
/// dic测试
/// </summary>
private static void DicTest()
{
List<string > li = new List<string>(1000);
Dictionary<int, string> di = new Dictionary<int, string>(1000);
for (int i = 0; i < 1000; i++)
{
li.Add("123");
}
Stopwatch sw = new Stopwatch();
sw.Stop();
Console.WriteLine("From list:- " + sw.ElapsedTicks);
sw.Restart();
for (int i = 0; i < 1000; i++)
{
di.Add(i,"123");
}
sw.Stop();
Console.WriteLine("From Dictionary:- " + sw.ElapsedTicks);
Console.ReadLine();
}
结果
3、for性能>foreach性能
代码
static void Main(string[] args)
{
ForTest();
}
/// <summary>
/// for测试
/// </summary>
private static void ForTest()
{
List<Int32> Count = new List<Int32>();
List<Int32> lst1 = new List<Int32>();
List<Int32> lst2 = new List<Int32>();
for (int i = 0; i < 10000; i++)
{
Count.Add(i);
}
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < Count.Count; i++)
{
lst1.Add(i);
}
sw.Stop();
Console.Write("For Loop :- " + sw.ElapsedTicks + "\n");
sw.Restart();
foreach (int a in Count)
{
lst2.Add(a);
}
sw.Stop();
Console.Write("Foreach Loop:- " + sw.ElapsedTicks);
Console.ReadLine();
}
结果
亲测结果
4、Model时,struct性能>class
代码
struct MyStructure
{
public string Name;
public string Surname;
}
class MyClass
{
public string Name;
public string Surname;
}
class Program
{
static void Main(string[] args)
{
MyStructure [] objStruct =new MyStructure[1000];
MyClass[] objClass = new MyClass[1000];
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 1000; i++)
{
objStruct[i] = newMyStructure();
objStruct[i].Name = "Sourav";
objStruct[i].Surname = "Kayal";
}
sw.Stop();
Console.WriteLine("For Structure:- "+ sw.ElapsedTicks);
sw.Restart();
for (int i = 0; i < 1000; i++)
{
objClass[i] = newMyClass();
objClass[i].Name = "Sourav";
objClass[i].Surname = "Kayal";
}
sw.Stop();
Console.WriteLine("For Class:- " + sw.ElapsedTicks);
Console.ReadLine();
}
}
结果
亲测结果
原因分析
struct是值类型,class是引用类型
5、字符串连接 Stringbuilder性能>String性能
代码
class Program
{
static void Main(string[] args)
{
String First = "A";
StringBuilder sb = new StringBuilder("A");
Stopwatch st = new Stopwatch();
st.Start();
for (int i = 0; i < 500; i++)
{
First = First + "A";
}
st.Stop();
Console.WriteLine("Using String :-" + st.ElapsedTicks);
st.Restart();
for (int i = 0; i < 500; i++)
{
sb.Append("A");
}
st.Stop();
Console.WriteLine("Using Stringbuilder :-" + st.ElapsedTicks);
Console.ReadLine();
}
}
结果
亲测结果
原因分析
StringBuilder采用的是双链结构
6、class成员中,String性能>string
代码
static void Main(string[] args)
{
StrTest();
}
/// <summary>
/// str测试
/// </summary>
private static void StrTest()
{
Stopwatch st = new Stopwatch();
st.Start();
for (int i = 0; i < 100; i++)
{
Test.Name = "Value";
}
st.Stop();
Console.WriteLine("Using Property: " + st.ElapsedTicks);
st.Restart();
for (int i = 0; i < 100; i++)
{
Test.surname = "Value";
}
st.Stop();
Console.WriteLine("Direct Assign: " + st.ElapsedTicks);
Console.ReadLine();
}
结果
亲测结果
7、循环外使用try catch 性能 > 循环内使用try catch
代码
static void Method1()
{
for (int i = 0; i < 1000; i++)
{
try
{
int value = i * 100;
if (value == -1)
{
throw new Exception();
}
}
catch
{
}
}
}
static void Method2()
{
try
{
for (int i = 0; i < 1000; i++)
{
int value = i * 100;
if (value == -1)
{
throw new Exception();
}
}
}
catch
{
}
}
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
Method1();
sw.Stop();
Console.WriteLine("Within Loop " + sw.ElapsedTicks);
sw.Restart();
Method2();
sw.Stop();
Console.WriteLine("Outside of Loop " + sw.ElapsedTicks);
Console.ReadLine();
}
亲测结果
8、集合查询性能 List<T> > 数组 > Dictionary<T, T>
代码
static void Main(string[] args)
{
List<Int32> li = new List<Int32>(1000);
Dictionary<int, int> di = new Dictionary<int, int>(1000);
int[] arr = new int[1000];
int a;
for (int i = 0; i < 1000; i++)
{
li.Add(i);
di.Add(i, i);
arr[i] = i;
}
Stopwatch sw = new Stopwatch();
sw.Start();
a = li[500];
sw.Stop();
Console.WriteLine("From list:- " + sw.ElapsedTicks);
sw.Start();
a = arr[500];
sw.Stop();
Console.WriteLine("From Integer array:- " + sw.ElapsedTicks);
sw.Restart();
a = di[500];
sw.Stop();
Console.WriteLine("From Dictionary:- " + sw.ElapsedTicks);
Console.ReadLine();
}
结果
亲测结果
9、方法对性能的影响
代码
static void Main(string[] args)
{
MethodTest();
}
/// <summary>
/// 方法测试
/// </summary>
private static void MethodTest()
{
Stopwatch sw = new Stopwatch();
sw.Start();
MethondA();
sw.Stop();
Console.WriteLine("I am Methond");
Console.WriteLine(sw.ElapsedTicks);
sw.Restart();
Console.WriteLine("I am single statement within main");
sw.Stop();
Console.WriteLine(sw.ElapsedTicks);
Console.ReadLine();
}
private static void MethondA()
{
MethondB();
}
private static void MethondB()
{
MethondC();
}
private static void MethondC()
{
Console.WriteLine("hello world");
}
结果
代码
static void Main(string[] args)
{
MethodTest();
}
/// <summary>
/// 方法测试
/// </summary>
private static void MethodTest()
{
Stopwatch sw = new Stopwatch();
sw.Start();
MethondA();
sw.Stop();
Console.WriteLine("I am Methond");
Console.WriteLine(sw.ElapsedTicks);
sw.Restart();
Console.WriteLine("I am single statement within main");
sw.Stop();
Console.WriteLine(sw.ElapsedTicks);
Console.ReadLine();
}
private static void MethondA()
{
MethondB();
}
private static void MethondB()
{
Console.WriteLine("hello world");
//MethondC();
}
结果
代码
static void Main(string[] args)
{
MethodTest();
}
/// <summary>
/// 方法测试
/// </summary>
private static void MethodTest()
{
Stopwatch sw = new Stopwatch();
sw.Start();
MethondA();
sw.Stop();
Console.WriteLine("I am Methond");
Console.WriteLine(sw.ElapsedTicks);
sw.Restart();
Console.WriteLine("I am single statement within main");
sw.Stop();
Console.WriteLine(sw.ElapsedTicks);
Console.ReadLine();
}
private static void MethondA()
{
Console.WriteLine("hello world");
//MethondB();
}
结果
总结
一些处理逻辑放在方法中使逻辑上更清楚,可同时是以牺牲性能为代价的,随着嵌套层的增多,
2.http://blog.csdn.net/xxdddail/article/details/11026289
1、数组添加性能>泛型List<T>
代码
staticvoid Main(string[] args)
{
List<int> li = new List<int>();
Stopwatch sw =new Stopwatch();
sw.Start();
for (int i = 0; i < 10000; i++)
{
li.Add(i);
}
sw.Stop();
Console.Write("Using Arraylist(Object)" + sw.ElapsedTicks + "\n");
sw.Reset();
sw.Start();
int[] a = new int[10000];
for (int i = 0; i < 10000; i++)
{
a[i] = i;
}
sw.Stop();
Console.Write("Using Value(Integer Array)" + sw.ElapsedTicks);
Console.ReadLine();
}
亲测结果
staticvoid Main(string[] args)
{
List<string> li = new List<string>();
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 10000; i++)
{
li.Add("123");
}
sw.Stop();
Console.Write("Using Arraylist(Object)" + sw.ElapsedTicks + "\n");
sw.Reset();
sw.Start();
string[] a = new string[10000];
for (int i = 0; i < 10000; i++)
{
a[i] = "123";
}
sw.Stop();
Console.Write("Using Value(Integer Array)" + sw.ElapsedTicks);
Console.ReadLine();
}
亲测结果
原因分析
List<T>会多次拆箱装箱(值类型和object之间转换),耗费大量的性能
2、List<T>性能 > Dictionary<T, T>性能
代码
static void Main(string[] args)
{
DicTest();
}
/// <summary>
/// dic测试
/// </summary>
private static void DicTest()
{
List<Int32> li = new List<Int32>(1000);
Dictionary<int, int> di = new Dictionary<int, int>(1000);
for (int i = 0; i < 1000; i++)
{
li.Add(i);
}
Stopwatch sw = new Stopwatch();
sw.Stop();
Console.WriteLine("From list:- " + sw.ElapsedTicks);
sw.Restart();
for (int i = 0; i < 1000; i++)
{
di.Add(i,i);
}
sw.Stop();
Console.WriteLine("From Dictionary:- " + sw.ElapsedTicks);
Console.ReadLine();
}
结果
static void Main(string[] args)
{
DicTest();
}
/// <summary>
/// dic测试
/// </summary>
private static void DicTest()
{
List<string > li = new List<string>(1000);
Dictionary<int, string> di = new Dictionary<int, string>(1000);
for (int i = 0; i < 1000; i++)
{
li.Add("123");
}
Stopwatch sw = new Stopwatch();
sw.Stop();
Console.WriteLine("From list:- " + sw.ElapsedTicks);
sw.Restart();
for (int i = 0; i < 1000; i++)
{
di.Add(i,"123");
}
sw.Stop();
Console.WriteLine("From Dictionary:- " + sw.ElapsedTicks);
Console.ReadLine();
}
结果
3、for性能>foreach性能
代码
static void Main(string[] args)
{
ForTest();
}
/// <summary>
/// for测试
/// </summary>
private static void ForTest()
{
List<Int32> Count = new List<Int32>();
List<Int32> lst1 = new List<Int32>();
List<Int32> lst2 = new List<Int32>();
for (int i = 0; i < 10000; i++)
{
Count.Add(i);
}
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < Count.Count; i++)
{
lst1.Add(i);
}
sw.Stop();
Console.Write("For Loop :- " + sw.ElapsedTicks + "\n");
sw.Restart();
foreach (int a in Count)
{
lst2.Add(a);
}
sw.Stop();
Console.Write("Foreach Loop:- " + sw.ElapsedTicks);
Console.ReadLine();
}
结果
亲测结果
4、Model时,struct性能>class
代码
struct MyStructure
{
public string Name;
public string Surname;
}
class MyClass
{
public string Name;
public string Surname;
}
class Program
{
static void Main(string[] args)
{
MyStructure [] objStruct =new MyStructure[1000];
MyClass[] objClass = new MyClass[1000];
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 1000; i++)
{
objStruct[i] = newMyStructure();
objStruct[i].Name = "Sourav";
objStruct[i].Surname = "Kayal";
}
sw.Stop();
Console.WriteLine("For Structure:- "+ sw.ElapsedTicks);
sw.Restart();
for (int i = 0; i < 1000; i++)
{
objClass[i] = newMyClass();
objClass[i].Name = "Sourav";
objClass[i].Surname = "Kayal";
}
sw.Stop();
Console.WriteLine("For Class:- " + sw.ElapsedTicks);
Console.ReadLine();
}
}
结果
亲测结果
原因分析
struct是值类型,class是引用类型
5、字符串连接 Stringbuilder性能>String性能
代码
class Program
{
static void Main(string[] args)
{
String First = "A";
StringBuilder sb = new StringBuilder("A");
Stopwatch st = new Stopwatch();
st.Start();
for (int i = 0; i < 500; i++)
{
First = First + "A";
}
st.Stop();
Console.WriteLine("Using String :-" + st.ElapsedTicks);
st.Restart();
for (int i = 0; i < 500; i++)
{
sb.Append("A");
}
st.Stop();
Console.WriteLine("Using Stringbuilder :-" + st.ElapsedTicks);
Console.ReadLine();
}
}
结果
亲测结果
原因分析
StringBuilder采用的是双链结构
6、class成员中,String性能>string
代码
static void Main(string[] args)
{
StrTest();
}
/// <summary>
/// str测试
/// </summary>
private static void StrTest()
{
Stopwatch st = new Stopwatch();
st.Start();
for (int i = 0; i < 100; i++)
{
Test.Name = "Value";
}
st.Stop();
Console.WriteLine("Using Property: " + st.ElapsedTicks);
st.Restart();
for (int i = 0; i < 100; i++)
{
Test.surname = "Value";
}
st.Stop();
Console.WriteLine("Direct Assign: " + st.ElapsedTicks);
Console.ReadLine();
}
结果
亲测结果
7、循环外使用try catch 性能 > 循环内使用try catch
代码
static void Method1()
{
for (int i = 0; i < 1000; i++)
{
try
{
int value = i * 100;
if (value == -1)
{
throw new Exception();
}
}
catch
{
}
}
}
static void Method2()
{
try
{
for (int i = 0; i < 1000; i++)
{
int value = i * 100;
if (value == -1)
{
throw new Exception();
}
}
}
catch
{
}
}
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
Method1();
sw.Stop();
Console.WriteLine("Within Loop " + sw.ElapsedTicks);
sw.Restart();
Method2();
sw.Stop();
Console.WriteLine("Outside of Loop " + sw.ElapsedTicks);
Console.ReadLine();
}
亲测结果
8、集合查询性能 List<T> > 数组 > Dictionary<T, T>
代码
static void Main(string[] args)
{
List<Int32> li = new List<Int32>(1000);
Dictionary<int, int> di = new Dictionary<int, int>(1000);
int[] arr = new int[1000];
int a;
for (int i = 0; i < 1000; i++)
{
li.Add(i);
di.Add(i, i);
arr[i] = i;
}
Stopwatch sw = new Stopwatch();
sw.Start();
a = li[500];
sw.Stop();
Console.WriteLine("From list:- " + sw.ElapsedTicks);
sw.Start();
a = arr[500];
sw.Stop();
Console.WriteLine("From Integer array:- " + sw.ElapsedTicks);
sw.Restart();
a = di[500];
sw.Stop();
Console.WriteLine("From Dictionary:- " + sw.ElapsedTicks);
Console.ReadLine();
}
结果
亲测结果
9、方法对性能的影响
代码
static void Main(string[] args)
{
MethodTest();
}
/// <summary>
/// 方法测试
/// </summary>
private static void MethodTest()
{
Stopwatch sw = new Stopwatch();
sw.Start();
MethondA();
sw.Stop();
Console.WriteLine("I am Methond");
Console.WriteLine(sw.ElapsedTicks);
sw.Restart();
Console.WriteLine("I am single statement within main");
sw.Stop();
Console.WriteLine(sw.ElapsedTicks);
Console.ReadLine();
}
private static void MethondA()
{
MethondB();
}
private static void MethondB()
{
MethondC();
}
private static void MethondC()
{
Console.WriteLine("hello world");
}
结果
代码
static void Main(string[] args)
{
MethodTest();
}
/// <summary>
/// 方法测试
/// </summary>
private static void MethodTest()
{
Stopwatch sw = new Stopwatch();
sw.Start();
MethondA();
sw.Stop();
Console.WriteLine("I am Methond");
Console.WriteLine(sw.ElapsedTicks);
sw.Restart();
Console.WriteLine("I am single statement within main");
sw.Stop();
Console.WriteLine(sw.ElapsedTicks);
Console.ReadLine();
}
private static void MethondA()
{
MethondB();
}
private static void MethondB()
{
Console.WriteLine("hello world");
//MethondC();
}
结果
代码
static void Main(string[] args)
{
MethodTest();
}
/// <summary>
/// 方法测试
/// </summary>
private static void MethodTest()
{
Stopwatch sw = new Stopwatch();
sw.Start();
MethondA();
sw.Stop();
Console.WriteLine("I am Methond");
Console.WriteLine(sw.ElapsedTicks);
sw.Restart();
Console.WriteLine("I am single statement within main");
sw.Stop();
Console.WriteLine(sw.ElapsedTicks);
Console.ReadLine();
}
private static void MethondA()
{
Console.WriteLine("hello world");
//MethondB();
}
结果
总结
一些处理逻辑放在方法中使逻辑上更清楚,可同时是以牺牲性能为代价的,随着嵌套层的增多,
性能的影响没有大幅变化,因此,确定把一块处理逻辑独立成第一层方法时,要好好权衡一下
下一篇地址:http://blog.csdn.net/xuwei_xuwei/article/details/21542957