using System using System Reflection using System Text using System Co

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Reflection;
using System.Text;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
namespace DynUnloop
{
// Проверка в цикле
class SearchLooping
{
public int Search(int[] a, int[] b)
{
int count = 0;
foreach (int i in b)
{ foreach (int j in a)
{ if (i == j)
{
count++;
break;
} } }
return count;
}
}
// Проверка cгенерированным switch'ом
public interface IChecker
{
bool Check(int valMax);
}
class SearchFlat
{
IChecker WriteCode(int[] a)
{
StringBuilder code = new StringBuilder();
code.Append("\n namespace DynUnloop" );
code.Append("\n { class Checker : IChecker" );
code.Append("\n { bool IChecker.Check(int n)");
code.Append("\n { switch (n)" );
code.Append("\n {" );
foreach (int j in a)
code.Append("\n case " + j + ":" );
code.Append("\n return true;" );
code.Append("\n }" );
code.Append("\n return false;" );
code.Append("\n } } }\n" );
//Console.Write(code.ToString()); // проверяем сгенерированный код
CSharpCodeProvider cs = new CSharpCodeProvider();
ICodeCompiler csc = cs.CreateCompiler();
CompilerParameters pars = new CompilerParameters();
pars.ReferencedAssemblies.Add(
Assembly.GetExecutingAssembly().Location);
CompilerResults result = csc.CompileAssemblyFromSource(
pars, code.ToString()); // компилируем!
if (result.Errors.Count != 0)
{
foreach(CompilerError err in result.Errors)
Console.WriteLine(err.ToString());
return null;
}
Assembly asm = Assembly.LoadFrom(result.PathToAssembly);
return (IChecker)asm.CreateInstance("DynUnloop.Checker");
}
public int Search(int[] arr1, int[] arr2)
{
if (this.code == null)
this.code = WriteCode(arr1);
int result = 0;
foreach (int i in arr2)
{
if (this.code.Check(i)) // используем сгенерированный код
result++;
}
return result;
}
IChecker code = null;
}
class Test
{
[STAThread]
static void Main(string[] args)
{
int[] a = { // Подсчитываем вхождения этих чисел в масссив arr2.
74, 97, 93, 86, 2, 78, 48, 14, 21, 58, 60, 5, 39, 4, 66, 9
, 31, 15, 69, 27, 37, 46, 62, 61, 81, 17, 88, 19, 44, 8
};
int[] b = { // В этом массиве ищем числа из, содержащиеся в arr1.
98, 53, 79, 47, 0, 39, 28, 18, 39, 49, 56, 17, 33, 19, 72, 13
, 28, 48, 21, 80, 10, 3, 67, 76, 83, 6, 40, 58, 23, 74, 81, 88
, 13, 48, 59, 83, 47, 1, 38, 63, 70, 21, 23, 30, 86, 71, 15, 25
, 32, 73, 23, 55, 52, 19, 90, 95, 84, 2, 63, 93, 98, 69, 93, 64
, 66, 66, 3, 84, 58, 88, 64, 26, 9, 56, 9, 88, 78, 37, 88, 11
, 89, 14, 26, 49, 50, 26, 36, 93, 56, 63, 97, 44, 37, 44, 64, 1
, 26, 58, 62, 19, 68, 30, 66, 42, 9, 96, 45, 94, 9, 2, 17, 46
, 12, 51, 3, 83, 43, 44, 14, 40, 30, 9, 27, 94, 90, 19, 87, 64
, 91, 8, 61, 20, 74, 69, 42, 59, 47, 82, 40, 52, 80, 41, 83, 54
, 45, 50, 31, 85, 41, 80, 56, 80, 44, 22, 88, 58, 3, 70, 51, 88
, 8, 80, 2, 1, 39, 96, 71, 42, 8, 43, 35, 59, 4, 60, 59, 88
, 25, 72, 48, 39, 86, 1, 23, 11, 50, 79, 74, 52, 79, 83, 56, 75
, 31, 50, 43, 0, 38, 82, 14, 48, 78, 88, 77, 97, 44, 96, 76, 83
, 61, 0, 32, 30, 22, 12, 1, 7, 56, 90, 49, 58, 21, 18, 62, 23
, 85, 58, 28, 52, 16, 58, 49, 42, 57, 98, 59, 97, 23, 25, 65, 53
, 3, 90, 89, 79, 50, 25, 53, 18, 49, 36, 42, 47, 33, 54, 27, 59
};
const int countIterations = 200000;
////////////////////////////////////////////
SearchLooping searchLoop = new SearchLooping();
DateTime start = DateTime.Now;
int count = 0;
for (int it = 0; it < countIterations; it++)
count = searchLoop.Search(a, b);
TimeSpan span = DateTime.Now - start;
Console.WriteLine(
"Search with looping. Count = {0}, Elapsed msec= {1}"
, count, span.TotalMilliseconds);
///////////////////////////////////////////
SearchFlat searchFlat = new SearchFlat();
DateTime start2 = DateTime.Now;
int count2 = 0;
for (int it = 0; it < countIterations; it++)
count2 = searchFlat.Search(a, b);
TimeSpan span2 = DateTime.Now - start2;
Console.WriteLine(
"Search with switch. Count = {0}, Elapsed msec= {1}"
, count2, span2.TotalMilliseconds);
Console.ReadLine();
}
}
}