using System.Collections;
using System.Diagnostics;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TSLab.Script.Helpers;
namespace TSLabTests {
[TestClass]
public class UnitTest1 {
static IList<double> Sma(IList<double> source, int period) {
var res = new double[source.Count];
var buffer = new double[period];
int j = 0, count = 0;
double prev = 0;
for (int i = 0; i < source.Count; i++) {
double v = source[i];
if (count >= period) {
prev += (v - buffer[j])/period;
count++;
} else
prev = (count*prev + v)/++count;
res[i] = prev;
buffer[j] = v;
if (++j >= period)
j = 0;
}
return res;
}
[TestMethod]
public void TestMethod1() {
// Для разогрева
var td = new double[] { 198500, 198515, 199600, 199740, 200000 };
var r1 = Series.SMA(td, 5);
var r2 = Sma(td, 5);
CollectionAssert.AreEqual(r1 as ICollection, r2 as ICollection);
// Сравнение длительности расчета
var sw = new Stopwatch();
const int count = (int) 1e8;
sw.Start();
for (int i = 0; i < count; i++)
Series.SMA(td, 5);
sw.Stop();
var t1 = sw.Elapsed;
sw.Restart();
for (int i = 0; i < count; i++)
Sma(td, 5);
sw.Stop();
var t2 = sw.Elapsed;
Trace.WriteLine("Быстрее на " + (t1 - t2) + " или на " + ((float) (t1.Ticks - t2.Ticks) / t1.Ticks * 100) + "%");
}
}
}