20元现金领取地址:http://jdb.jiudingcapital.com/phone.html
内部邀请码:C8E245J (不写邀请码,没有现金送)
国内私募机构九鼎控股打造,九鼎投资是在全国股份转让系统挂牌的公众公司,股票代码为430719,为“中国PE第一股”,市值超1000亿元。
循环优化,是程序在最短时间内运行出结果,大部分思想是空间换时间,但是这
个方法还是比较特别的。
直接看代码说话:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
循环测试
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
namespace ConsoleTest
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
7
class Program
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
const string CONSOLE_EXIT = "exit";
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
static void Main(string[] args)
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
PerformanceMonitor pm = new PerformanceMonitor();
14
pm.OnCallMethod += new PerformanceMonitor.CallMethod(test);
15![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
Console.WriteLine("请按照格式 [整数1,整数2] 输入数据 (输入
17![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{0} 结束)
", CONSOLE_EXIT);
19![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
// 循环提示输入数据
21
int outter = 0;
22
int inner = 0;
23
string str = string.Empty;
24
while(true)
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
26
str = Console.ReadLine();
27
if (str.Trim().ToLower().Equals(CONSOLE_EXIT))
28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
29
break;
30
}
31![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
if (str.IndexOf(',') < 1)
33![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
34
continue;
35
}
36![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
outter = int.Parse(str.Split(new char[]
{',' })[0]);
37![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
inner = int.Parse(str.Split(new char[]
{ ',' })[1]);
38![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
pm.Test(outter, inner);
40
}
41![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
Console.WriteLine("Press any key to continue
");
43
Console.ReadLine();
44
}
45![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
47
/// 测试函数
48
/// </summary>
49
/// <param name="outter">外层循环</param>
50
/// <param name="inner">内层循环</param>
51
private static void test(int outter, int inner)
52![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
53
StringBuilder builder = new StringBuilder();
54
// 测试主体部分
55
for (int i = 0; i < outter; i++)
56![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
57
// 外层循环代码块
58
builder.AppendFormat("Outer counter: {0}
59![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
\t\n",i.ToString());
61
for (int j = 0; j < inner; j++)
62![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
63
// 内层循环代码块
64
builder.AppendFormat("Inner counter: {0}\t",
65![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
j.ToString());
67
}
68
builder.Append("\n");
69
}
70![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
Console.WriteLine(builder.ToString());
72
}
73
}
74![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
public class PerformanceMonitor
76![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
77
// 定制事件
78
public delegate void CallMethod(int outter, int inner);
79
public event CallMethod OnCallMethod;
80
// 记录时间
81
private double mCostTime = 0;
82![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
83![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
84
/// 调用函数进行测试
85
/// </summary>
86
/// <param name="outter">外层数据</param>
87
/// <param name="inner">内层数据</param>
88
public void WriteText(int outter, int inner)
89![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
90
DateTime start = DateTime.Now;
91
if (null != this.OnCallMethod)
92![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
93
this.OnCallMethod(outter, inner);
94
}
95
DateTime end = DateTime.Now;
96
this.mCostTime = end.Subtract(start).TotalMilliseconds;
97
}
98![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
99![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
100
/// 显示测试信息
101
/// </summary>
102
/// <param name="o">数据一</param>
103
/// <param name="i">数据二</param>
104
public void Test(int o, int i)
105![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
106
StringBuilder builder = new StringBuilder("\t外层\t内层\t耗
107![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
108
时(ms)\n");
109
int max = 0;
110
int min = 0;
111
double time1 = 0;
112
double time2 = 0;
113![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
114
// 确定数据大小值
115
max = (o >= i) ? o : i;
116
min = (max == o) ? i : o;
117![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
118
// 大数在 外层 循环
119
this.WriteText(max, min);
120
time1 = this.mCostTime;
121
builder.AppendFormat("\t{0}\t{1}\t{2}\n", max.ToString(),
122![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
123
min.ToString(), time1.ToString("#.##"));
124![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
125
// 大数在 内层 循环
126
this.WriteText(min, max);
127
time2 = this.mCostTime;
128
builder.AppendFormat("\t{0}\t{1}\t{2}\n", min.ToString(),
129![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
130
max.ToString(), time2.ToString("#.##"));
131![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
132
// 记录时间和效率
133
double ms = time1 - time2;
134
double gs = (1 - time2/time1) * 100;
135
builder.AppendFormat("优化:\t节时 {0} ms\t效率 {1}%",
136![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
137
(ms.ToString("#.##").Length < 1) ? "0.00" : ms.ToString("#.##"),
138![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
139
(gs.ToString("#.##").Length < 1) ? "0.00" : gs.ToString("#.##"));
140![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
141
// 用颜色区分一下
142
Console.ForegroundColor = ConsoleColor.Red;
143
Console.WriteLine(builder.ToString());
144
Console.ResetColor();
145
}
146![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
147
}
148
}
149![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
代码内注释已经比较详细了,就不多说了。只看结果 [ 测试结果不能重复,依据
配置和工具不同而不同 ]:
Outter Inner Time(ms)
1000 100 20960.14
100 1000 941.35
efficiency: 20018.79 ms 95.51%
Outter Inner Time(ms)
300 20 2763.97
20 300 1932.78
efficiency: 831.2 ms 30.07%
优化结果令人吃惊,以后写代码的时候一定要注意哦。不过根据本人测试结果分析,内、外层差距越大,优化结果越明显。
所以,此种方法的优化的条件个人总结为:
1、内、外层循环差距在3倍以上;
2、内外层的总数最好都在10以上,这样测试才更有优化的效果,小数据量看不出
变化的哦。至少我这里是看不出来。