循环优化,是程序在最短时间内运行出结果,大部分思想是空间换时间,但是这个方法还是比较特别的。
直接看代码说话:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleTest
{
class Program
{
const string CONSOLE_EXIT = "exit";
static void Main(string[] args)
{
PerformanceMonitor pm = new PerformanceMonitor();
pm.OnCallMethod += new PerformanceMonitor.CallMethod(test);
Console.WriteLine("请按照格式 [整数1,整数2] 输入数据 (输入 {0} 结束) ...", CONSOLE_EXIT);
// 循环提示输入数据
int outter = 0;
int inner = 0;
string str = string.Empty;
while(true)
{
str = Console.ReadLine();
if (str.Trim().ToLower().Equals(CONSOLE_EXIT))
{
break;
}
if (str.IndexOf(',') < 1)
{
continue;
}
outter = int.Parse(str.Split(new char[] {',' })[0]);
inner = int.Parse(str.Split(new char[] { ',' })[1]);
pm.Test(outter, inner);
}
Console.WriteLine("Press any key to continue ...");
Console.ReadLine();
}
/// <summary>
/// 测试函数
/// </summary>
/// <param name="outter">外层循环</param>
/// <param name="inner">内层循环</param>
private static void test(int outter, int inner)
{
StringBuilder builder = new StringBuilder();
// 测试主体部分
for (int i = 0; i < outter; i++)
{
// 外层循环代码块
builder.AppendFormat("Outer counter: {0}/t/n",i.ToString());
for (int j = 0; j < inner; j++)
{
// 内层循环代码块
builder.AppendFormat("Inner counter: {0}/t", j.ToString());
}
builder.Append("/n");
}
Console.WriteLine(builder.ToString());
}
}
public class PerformanceMonitor
{
// 定制事件
public delegate void CallMethod(int outter, int inner);
public event CallMethod OnCallMethod;
// 记录时间
private double mCostTime = 0;
/// <summary>
/// 调用函数进行测试
/// </summary>
/// <param name="outter">外层数据</param>
/// <param name="inner">内层数据</param>
public void WriteText(int outter, int inner)
{
DateTime start = DateTime.Now;
if (null != this.OnCallMethod)
{
this.OnCallMethod(outter, inner);
}
DateTime end = DateTime.Now;
this.mCostTime = end.Subtract(start).TotalMilliseconds;
}
/// <summary>
/// 显示测试信息
/// </summary>
/// <param name="o">数据一</param>
/// <param name="i">数据二</param>
public void Test(int o, int i)
{
StringBuilder builder = new StringBuilder("/t外层/t内层/t耗时(ms)/n");
int max = 0;
int min = 0;
double time1 = 0;
double time2 = 0;
// 确定数据大小值
max = (o >= i) ? o : i;
min = (max == o) ? i : o;
// 大数在 外层 循环
this.WriteText(max, min);
time1 = this.mCostTime;
builder.AppendFormat("/t{0}/t{1}/t{2}/n", max.ToString(), min.ToString(), time1.ToString("#.##"));
// 大数在 内层 循环
this.WriteText(min, max);
time2 = this.mCostTime;
builder.AppendFormat("/t{0}/t{1}/t{2}/n", min.ToString(), max.ToString(), time2.ToString("#.##"));
// 记录时间和效率
double ms = time1 - time2;
double gs = (1 - time2/time1) * 100;
builder.AppendFormat("优化:/t节时 {0} ms/t效率 {1}%", (ms.ToString("#.##").Length < 1) ? "0.00" : ms.ToString("#.##"), (gs.ToString("#.##").Length < 1) ? "0.00" : gs.ToString("#.##"));
// 用颜色区分一下
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(builder.ToString());
Console.ResetColor();
}
}
}
代码内注释已经比较详细了,就不多说了。只看结果 [ 测试结果不能重复,依据配置和工具不同而不同 ]:
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以上,这样测试才更有优化的效果,小数据量看不出变化的哦。至少我这里是看不出来。