diff --git a/hw2/graph.py b/hw2/graph.py new file mode 100644 index 0000000..e2b472d --- /dev/null +++ b/hw2/graph.py @@ -0,0 +1,22 @@ +import numpy as np +import pandas as pd +import matplotlib.pyplot as plt + +test_data = open("time.txt", "r") +lines = test_data.readlines() +time = [] +n = [] +for line in lines: + line = line.strip('\n ] [ ').split(' ') + time.append(int(line[1])) + n.append(int(line[0])) +test_data.close() +time.sort() +n.sort() +x = n[:-1] +y = time[:-1] +plt.plot(x, y, label="the relationship between N and the execution time") +plt.legend() +plt.xlabel("N") +plt.ylabel("time") +plt.savefig('time.png') diff --git a/hw2/matrix.go b/hw2/matrix.go index 058c603..17eac2c 100644 --- a/hw2/matrix.go +++ b/hw2/matrix.go @@ -16,6 +16,12 @@ func newMatrix(n int) [][]int { return m } +func failOnError(err error) { + if err != nil { + log.Fatal("Error:", err) + } +} + func main() { if len(os.Args) < 2 { fmt.Println("usage: go run matrix.go N") @@ -85,4 +91,18 @@ func main() { if want != 0 && total != want { fmt.Printf("Wanted sum %v but got %v\n", want, total) } + + var n_str string + var time_str string + n_str = strconv.Itoa(n) + time_str = strconv.Itoa(int(end.Sub(begin))) + + file, err := os.OpenFile("time.txt", os.O_WRONLY|os.O_APPEND, 0600) + if err != nil { + log.Fatal(err) + } + defer file.Close() + + fmt.Fprintln(file, []string{n_str, time_str}) + } diff --git a/hw2/time.png b/hw2/time.png new file mode 100644 index 0000000..bf98621 Binary files /dev/null and b/hw2/time.png differ diff --git a/hw2/time.txt b/hw2/time.txt new file mode 100644 index 0000000..60a7edd --- /dev/null +++ b/hw2/time.txt @@ -0,0 +1,22 @@ +[1 276] +[2 334] +[3 537] +[4 473] +[5 633] +[6 861] +[7 1207] +[8 1701] +[9 2318] +[10 4244] +[50 351532] +[100 3060330] +[200 27809659] +[300 122514780] +[400 302434663] +[400 280376609] +[500 621763472] +[600 1039208850] +[700 2155400336] +[800 2794530550] +[900 11907044262] +[1000 15037068087]