using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace RUNPCCPUTest
{
class Program
{
private static String _doc =
"CGAATCTAAAAATAGATTCGGACGTGATGTAGTCGTACAAATGAAAAAGTAAGCC";
private static int ITERATIONS = 1000000;
public static void Main()
{
String nl = Environment.NewLine;
String[] colorNames = ConsoleColor.GetNames(typeof(ConsoleColor));
Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor),"White");
Console.WriteLine("此範例會計算從1000000~2000000運算字串配對,等候到在2000000找到結果才會結束"+"\r\n");
Console.ResetColor();
//設定開始時間
long start = System.DateTime.Now.Ticks / 10000;
long end;
int length = 1;
int x = 9;
for (int i = ITERATIONS; i <= ITERATIONS * 2; i++)
{
length = (int)(Math.Log((double)i) / Math.Log(4));
String matchthis = generateWord(i, length + 1);
Regex regexpr = new Regex(matchthis, RegexOptions.Compiled);
Boolean b = regexpr.IsMatch(_doc);
//成功配對字串
if (b)
{
//設定結束時間
end = System.DateTime.Now.Ticks / 10000;
Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorNames[x]);
x++;
if (x == 15)
x = 9;
Console.WriteLine("在 {0} 找到 {1},共花了 {2} 毫秒.",
i, matchthis, end - start);
Console.ResetColor();
}
}
//設定結束時間
end = System.DateTime.Now.Ticks / 10000;
Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), "White");
Console.WriteLine("\r\n" + ".NET 常規表示法運算共花了 {0} 毫秒", end - start);
Console.ResetColor();
}
///
/// 產生字串
///
///
///
///
public static String generateWord(int value, int length)
{
StringBuilder buf = new StringBuilder();
int current = value;
for (int i = 0; i < length; i++)
{
int v = current % 4;
current = current / 4;
buf.Append(convert(v));
}
return buf.ToString();
}
///
/// 轉換為英文字母
///
///
///
private static String convert(int value)
{
switch (value)
{
case 0: return "A";
case 1: return "G";
case 2: return "T";
case 3: return "C";
default:
return "0";
}
}
}
}