update analysis
This commit is contained in:
parent
13fa8fcdae
commit
8f4473f8fa
@ -1,5 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace Lab2
|
||||
{
|
||||
@ -57,7 +60,15 @@ namespace Lab2
|
||||
return n;
|
||||
}
|
||||
|
||||
public static double TestFunc(Func<int[], long> T, uint n, int value)
|
||||
public static double TimeFunc(Action Func)
|
||||
{
|
||||
var before = DateTime.Now;
|
||||
Func();
|
||||
var duration = DateTime.Now - before;
|
||||
return duration.TotalSeconds;
|
||||
}
|
||||
|
||||
public static double TestFunc<R>(Func<int[], R> T, uint n, int value)
|
||||
{
|
||||
int[] data = new int[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
@ -65,11 +76,8 @@ namespace Lab2
|
||||
data[i] = value;
|
||||
}
|
||||
|
||||
var before = DateTime.Now;
|
||||
counter = 0;
|
||||
T(data);
|
||||
var duration = DateTime.Now - before;
|
||||
return duration.TotalSeconds;
|
||||
return TimeFunc(() => T(data));
|
||||
}
|
||||
|
||||
static void TestMethod(Func<int[], long> F, string format, int bestValue, uint[] best, int worstValue, uint[] worst)
|
||||
@ -88,13 +96,73 @@ namespace Lab2
|
||||
}
|
||||
}
|
||||
|
||||
static uint Recursive(Span<uint> board)
|
||||
{
|
||||
if (board.Length <= 1)
|
||||
return 0;
|
||||
else if (board.Length == 2)
|
||||
return board[1];
|
||||
|
||||
uint score1 = board[1] + Recursive(board[1..]);
|
||||
uint score2 = 2 * (uint)Math.Abs((int)board[0] - (int)board[2]) + Recursive(board[2..]);
|
||||
|
||||
return Math.Max(score1, score2);
|
||||
}
|
||||
|
||||
static uint DP(uint[] board)
|
||||
{
|
||||
uint[] cache = new uint[board.Length];
|
||||
cache[0] = 0;
|
||||
cache[1] = board[1];
|
||||
|
||||
for (int i = 2; i < board.Length; i++)
|
||||
{
|
||||
uint score1 = board[i] + cache[i - 1];
|
||||
uint score2 = 2 * (uint)Math.Abs((int)board[i] - (int)board[i - 2]) + cache[i - 2];
|
||||
|
||||
cache[i] = Math.Max(score1, score2);
|
||||
}
|
||||
|
||||
return cache[board.Length - 1];
|
||||
}
|
||||
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
string format = " F({0}) => {1}s {2}i";
|
||||
//string format = "{1} {2}";
|
||||
|
||||
string format = " Recursive({0}) => {1}s";
|
||||
foreach (var size in new uint[] { 10, 30, 35, 37 })
|
||||
{
|
||||
uint[] board = new uint[size];
|
||||
for (uint i = 0; i < size; i++)
|
||||
{
|
||||
board[i] = i;
|
||||
}
|
||||
|
||||
double time = TimeFunc(() => Recursive(board));
|
||||
Console.WriteLine(format, size, time, counter);
|
||||
}
|
||||
|
||||
|
||||
format = " DP({0}) => {1}s";
|
||||
foreach (var size in new uint[] { 1000, 10000000, 30000000, 60000000, 180000000 })
|
||||
{
|
||||
uint[] board = new uint[size];
|
||||
for (uint i = 0; i < size; i++)
|
||||
{
|
||||
board[i] = i;
|
||||
}
|
||||
|
||||
double time = TimeFunc(() => DP(board));
|
||||
Console.WriteLine(format, size, time, counter);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
|
||||
//string format = "{1} {2}";
|
||||
|
||||
Console.WriteLine("methodToAnalysis1:");
|
||||
TestMethod(
|
||||
methodToAnalysis1,
|
||||
@ -102,7 +170,7 @@ namespace Lab2
|
||||
-1, new uint[] { 10000, 100000000, 300000000, 600000000, 900000000 },
|
||||
1, new uint[] { 500, 600, 700, 800, 1000 }
|
||||
);
|
||||
*/
|
||||
|
||||
|
||||
Console.WriteLine("methodToAnalysis2:");
|
||||
TestMethod(
|
||||
@ -111,6 +179,7 @@ namespace Lab2
|
||||
1, new uint[] { 10000, 100000000, 300000000, 600000000, 900000000 },
|
||||
-1, new uint[] { 10000, 20000, 30000, 50000, 100000 }
|
||||
);
|
||||
*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user