■ R과 NMATH 계산 시간을 비교하는 방법을 보여준다.
▶ MainForm.cs
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
using System; using System.Diagnostics; using System.IO; using System.Linq; using System.Windows.Forms; using RDotNet; using CenterSpace.NMath.Core; using CenterSpace.NMath.Stats; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 소스 값 배열 1 /// </summary> private double[] sourceValueArray1; /// <summary> /// 소스 값 배열 2 /// </summary> private double[] sourceValueArray2; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); string pathValue = Environment.GetEnvironmentVariable("Path"); string rBinarayPath = @"C:\Program Files\R\R-3.4.1\bin\x64;C:\Program Files\R\R-3.4.1\bin\i386"; Environment.SetEnvironmentVariable("Path", pathValue + Path.PathSeparator + rBinarayPath); this.runButton.Click += runButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 실행 버튼 클릭시 처리하기 - runButton_Click(sender, e) /// <summary> /// 실행 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void runButton_Click(object sender, EventArgs e) { this.sourceValueArray1 = GetSourceValueArray(1000000); this.sourceValueArray2 = GetSourceValueArray(1000000); CacluateWithR(); CalculateWithNMATH(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 소스 값 배열 구하기 - GetSourceValueArray(count) /// <summary> /// 소스 값 배열 구하기 /// </summary> /// <param name="count">카운트</param> /// <returns>소스 값 배열</returns> private double[] GetSourceValueArray(int count) { double[] valueArray = new double[count]; Random random = new Random(DateTime.Now.Millisecond); for(int i = 0; i < count; i++) { valueArray[i] = random.NextDouble() * 100; } return valueArray; } #endregion #region 계산하기 (R) - CacluateWithR() /// <summary> /// 계산하기 (R) /// </summary> private void CacluateWithR() { Stopwatch stopWatch = new Stopwatch(); using(REngine rEngine = REngine.CreateInstance("RDotNet")) { rEngine.Initialize(); #region 데이터를 변환한다. stopWatch.Start(); //NumericVector numericVector1 = rEngine.CreateNumericVector(this.sourceValueArray1); //NumericVector numericVector2 = rEngine.CreateNumericVector(this.sourceValueArray2); NumericVector numericVector1 = new NumericVector(rEngine, this.sourceValueArray1); NumericVector numericVector2 = new NumericVector(rEngine, this.sourceValueArray2); stopWatch.Stop(); long dataConversionTime = stopWatch.ElapsedMilliseconds; #endregion rEngine.SetSymbol("group1", numericVector1); rEngine.SetSymbol("group2", numericVector2); #region 평균 값을 계산한다. stopWatch.Restart(); double averageValue = rEngine.Evaluate("mean(group1)").AsNumeric().First(); stopWatch.Stop(); long averageValueTime = stopWatch.ElapsedMilliseconds; #endregion #region 중간 값을 계산한다. stopWatch.Restart(); double medianValue = rEngine.Evaluate("median(group1)").AsNumeric().First(); stopWatch.Stop(); long medianValueTime = stopWatch.ElapsedMilliseconds; #endregion #region 표준 편차 값을 계산한다. stopWatch.Restart(); double standardDeviationValue = rEngine.Evaluate("sd(group1)").AsNumeric().First(); stopWatch.Stop(); long standardDeviationValueTime = stopWatch.ElapsedMilliseconds; #endregion #region 상관 계수 값을 계산한다. stopWatch.Restart(); double correlationValue = rEngine.Evaluate("cor(group1, group2)").AsNumeric().First(); stopWatch.Stop(); long correlationValueTime = stopWatch.ElapsedMilliseconds; #endregion #region ANOVA 값을 계산한다. stopWatch.Restart(); GenericVector genericVector = rEngine.Evaluate("t.test(group1, group2)").AsList(); double pValue = genericVector["p.value"].AsNumeric().First(); stopWatch.Stop(); long pValueTime = stopWatch.ElapsedMilliseconds; #endregion this.dataConversionTimeTextBox1.Text = dataConversionTime.ToString(); this.avergageValueTextBox1.Text = averageValue.ToString(); this.averageValueTimeTextBox1.Text = averageValueTime.ToString(); this.medianValueTextBox1.Text = medianValue.ToString(); this.medianValueTimeTextBox1.Text = medianValueTime.ToString(); this.standardDeviationValueTextBox1.Text = standardDeviationValue.ToString(); this.standardDeviationValueTimeTextBox1.Text = standardDeviationValueTime.ToString(); this.correlationValueTextBox1.Text = correlationValue.ToString(); this.correlationValueTimeTextBox1.Text = correlationValueTime.ToString(); this.anovaValueTextBox1.Text = pValue.ToString(); this.anovaValueTimeTextBox1.Text = pValueTime.ToString(); } } #endregion #region 계산하기 (NMATH) - CalculateWithNMATH() /// <summary> /// 계산하기 (NMATH) /// </summary> private void CalculateWithNMATH() { Stopwatch stopwatch = new Stopwatch(); #region 데이터를 변환한다. stopwatch.Start(); DoubleVector doubleVector1 = new DoubleVector(this.sourceValueArray1); DoubleVector doubleVector2 = new DoubleVector(this.sourceValueArray2); stopwatch.Stop(); long dataConversionTime = stopwatch.ElapsedMilliseconds; #endregion #region 평균 값을 계산한다. stopwatch.Restart(); double averageValue = NMathFunctions.Mean(doubleVector1); stopwatch.Stop(); long averageValueTime = stopwatch.ElapsedMilliseconds; #endregion #region 중간 값을 계산한다. stopwatch.Restart(); double medianValue = NMathFunctions.Median(doubleVector1); stopwatch.Stop(); long medianValueTime = stopwatch.ElapsedMilliseconds; #endregion #region 표준 편차 값을 계산한다. stopwatch.Restart(); double standardDeviationValue = StatsFunctions.StandardDeviation(doubleVector1); stopwatch.Stop(); long standardDeviationValueTime = stopwatch.ElapsedMilliseconds; #endregion #region 상관 계수 값을 계산한다. stopwatch.Restart(); double correlationValue = StatsFunctions.Correlation(doubleVector1, doubleVector2); stopwatch.Stop(); long correlationValueTime = stopwatch.ElapsedMilliseconds; #endregion #region ANOVA 값을 계산한다. stopwatch.Restart(); OneWayAnova oneWayAnova = new OneWayAnova(); oneWayAnova.SetData(new DoubleVector[] { doubleVector1, doubleVector2 }); double pValue = oneWayAnova.AnovaTable.FStatisticPValue; stopwatch.Stop(); long pValueTime = stopwatch.ElapsedMilliseconds; #endregion this.dataConversionTimeTextBox2.Text = dataConversionTime.ToString(); this.averageValueTextBox2.Text = averageValue.ToString(); this.averageValueTimeTextBox2.Text = averageValueTime.ToString(); this.medianValueTextBox2.Text = medianValue.ToString(); this.medianValueTimeTextBox2.Text = medianValueTime.ToString(); this.standardDeviationValueTextBox2.Text = standardDeviationValue.ToString(); this.standardDeviationValueTimeTextBox2.Text = standardDeviationValueTime.ToString(); this.correlationValueTextBox2.Text = correlationValue.ToString(); this.correlationValueTimeTextBox2.Text = correlationValueTime.ToString(); this.anovaValueTextBox.Text = pValue.ToString(); this.anovaValueTimeTextBox.Text = pValueTime.ToString(); } #endregion } } |