Created
March 18, 2014 09:56
-
-
Save frp/9616967 to your computer and use it in GitHub Desktop.
C# Task17
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Diagnostics; | |
namespace Task17_cs | |
{ | |
class Program | |
{ | |
const int arraySize = 20000; | |
const int matrixSize = 600; | |
static void Main(string[] args) | |
{ | |
testA(); | |
testB(); | |
testC(); | |
Console.ReadLine(); | |
} | |
private static void testC() | |
{ | |
Console.WriteLine("Test C"); | |
Stopwatch sw = new Stopwatch(); | |
sw.Start(); | |
Random r = new Random(); | |
double[,] a = new double[matrixSize, matrixSize]; | |
double[,] b = new double[matrixSize, matrixSize]; | |
double[,] c = new double[matrixSize, matrixSize]; | |
for (int i = 0; i < matrixSize; i++) | |
for (int j = 0; j < matrixSize; j++) | |
{ | |
a[i, j] = r.NextDouble() / 10 + 1; | |
b[i, j] = r.NextDouble() / 10 + 1; | |
} | |
for (int i = 0; i < matrixSize; i++) | |
for (int j = 0; j < matrixSize; j++) | |
for (int k = 0; k < matrixSize; k++) | |
c[i, j] += a[i, k] * b[k, j]; | |
sw.Stop(); | |
Console.WriteLine("Consumed time: {0}", sw.Elapsed); | |
} | |
private static void testB() | |
{ | |
Console.WriteLine("Test B"); | |
Stopwatch sw = new Stopwatch(); | |
sw.Start(); | |
Random r = new Random(); | |
double[] a = new double[arraySize]; | |
for (int i = 0; i < arraySize; i++) | |
a[i] = r.NextDouble() / 10 + 1; | |
double sum = 0; | |
for (int i = 0; i < arraySize; i++) | |
for (int j = 0; j < arraySize; j++) | |
sum += 1 / (a[i] * a[j]); | |
sw.Stop(); | |
Console.WriteLine("Result: {0}, Consumed time: {1}", sum, sw.Elapsed); | |
} | |
private static void testA() | |
{ | |
Console.WriteLine("Task A"); | |
Stopwatch sw = new Stopwatch(); | |
sw.Start(); | |
double sum = 0; | |
for (int i = 1; i <= arraySize; i++) | |
for (int j = 1; j <= arraySize; j++) | |
sum += 1.0 / (i * j); | |
sw.Stop(); | |
Console.WriteLine("Result: {0}, Consumed time: {1}", sum, sw.Elapsed); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment