一般我们发布项目的时候通常都会采用release版本,因为release会在jit层面对我们的il代码进行了优化,比如在迭代和内存操作的性能提升方面,废话不多说,
我先用一个简单的“冒泡排序”体验下release和debug下面的性能差距。
一:release带来的闪光点【冒泡排序】
这个是我多年前写的算法系列中的一个冒泡排序的例子,就随手翻出来展示一下,准备灌入50000条数据,这样就可以执行25亿次迭代,王健林说,不能太张
狂,几十亿对我来说不算小意思,算中等意思吧。
1 namespace ConsoleApplication4 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 var rand = new Random(); 8 List<int> list = new List<int>(); 9 10 for (int i = 0; i < 50000; i++) 11 { 12 list.Add(rand.Next()); 13 } 14 15 var watch = Stopwatch.StartNew(); 16 17 try 18 { 19 BubbleSort(list); 20 } 21 catch (Exception ex) 22 { 23 Console.WriteLine(ex.Message); 24 } 25 26 watch.Stop(); 27 28 Console.WriteLine("耗费时间:{0}", watch.Elapsed); 29 } 30 31 //冒泡排序算法 32 static List<int> BubbleSort(List<int> list) 33 { 34 int temp; 35 //第一层循环: 表明要比较的次数,比如list.count个数,肯定要比较count-1次 36 for (int i = 0; i < list.Count - 1; i++) 37 { 38 //list.count-1:取数据最后一个数下标, 39 //j>i: 从后往前的的下标一定大于从前往后的下标,否则就超越了。 40 for (int j = list.Count - 1; j > i; j--) 41 { 42 //如果前面一个数大于后面一个数则交换 43