From 7b40a4243d163f89693fb9d00c99e02034ed6e0e Mon Sep 17 00:00:00 2001 From: Trygve Date: Tue, 5 Mar 2024 10:35:30 +0100 Subject: [PATCH] =?UTF-8?q?"Ferdig"=20med=20=C3=B8ving=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab_2/11/memleak.cpp | 2 +- lab_2/12/Makefile | 2 +- lab_2/13/Makefile | 2 +- lab_2/13/memleak.cpp | 8 +++++++- lab_2/13/plot_elongation.py | 30 ++++++++++++++++++++++++++++++ lab_2/15.cpp | 23 +++++++++++++++++++++++ lab_2/15.py | 11 +++++++++++ 7 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 lab_2/13/plot_elongation.py create mode 100644 lab_2/15.cpp create mode 100644 lab_2/15.py diff --git a/lab_2/11/memleak.cpp b/lab_2/11/memleak.cpp index 5833225..08c9583 100644 --- a/lab_2/11/memleak.cpp +++ b/lab_2/11/memleak.cpp @@ -49,7 +49,7 @@ int main(int argc, char** argv) // Simple fix to memleak: float* old_configuration = present_configuration; present_configuration = crw::step(size, present_configuration); - delete old_configuration; + delete[] old_configuration; float present_elongation = crw::elongation(size, present_configuration); if(present_elongation > extreme_elongation) diff --git a/lab_2/12/Makefile b/lab_2/12/Makefile index 6040f26..1b6d9c3 100644 --- a/lab_2/12/Makefile +++ b/lab_2/12/Makefile @@ -6,7 +6,7 @@ run: $(binary) ./$(binary) 7 100000000 $(binary): $(objects) - g++ -o $@ $^ + g++ -Ofast -march=native -flto -fno-signed-zeros -fno-trapping-math -o $@ $^ clean: rm -f *.o diff --git a/lab_2/13/Makefile b/lab_2/13/Makefile index 6040f26..1b6d9c3 100644 --- a/lab_2/13/Makefile +++ b/lab_2/13/Makefile @@ -6,7 +6,7 @@ run: $(binary) ./$(binary) 7 100000000 $(binary): $(objects) - g++ -o $@ $^ + g++ -Ofast -march=native -flto -fno-signed-zeros -fno-trapping-math -o $@ $^ clean: rm -f *.o diff --git a/lab_2/13/memleak.cpp b/lab_2/13/memleak.cpp index 08c9583..d4b04fc 100644 --- a/lab_2/13/memleak.cpp +++ b/lab_2/13/memleak.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include "chain-random-walk.h" @@ -61,6 +62,11 @@ int main(int argc, char** argv) el_avg = el_sum / (i + 1.0); } crw::output(steps, size, present_configuration, extreme_configuration, el_avg); - + + std::ofstream myfile; + myfile.open ("elongation.csv", std::ios::app); + myfile << size << ", " << el_avg << ", " << crw::elongation(size, extreme_configuration) << "\n"; + myfile.close(); + delete[] present_configuration; } diff --git a/lab_2/13/plot_elongation.py b/lab_2/13/plot_elongation.py new file mode 100644 index 0000000..3e29aec --- /dev/null +++ b/lab_2/13/plot_elongation.py @@ -0,0 +1,30 @@ +import matplotlib.pyplot as plt +import matplotlib.cbook as cbook + +import numpy as np +import pandas as pd + +f = open('elongation.csv', 'r') +x = [] +y_average = [] +y_extreme = [] +for l in f.readlines(): + l = l.split(',') + x.append(l[0]) + y_average.append(float(l[1])) + y_extreme.append(float(l[2])) + +plt.plot(x, y_average, label = 'average') +plt.plot(x, y_extreme, label = 'extreme') + +for i in range(len(x)): + plt.text(i,y_average[i],f'{y_average[i]:.2f}', color="blue") +for i in range(len(x)): + plt.text(i,y_extreme[i],f'{y_extreme[i]:.2f}', color="orange") + +plt.title('Speedup after optimization') +plt.xlabel('N') +plt.ylabel('elongation') +plt.grid() +plt.legend() +plt.show() diff --git a/lab_2/15.cpp b/lab_2/15.cpp new file mode 100644 index 0000000..038702e --- /dev/null +++ b/lab_2/15.cpp @@ -0,0 +1,23 @@ +#include +#include +#include +#include +int main() +{ + auto t0 = std::chrono::high_resolution_clock::now(); + //std::ofstream myfile; + + std::vector list; + int data = 1024; + int n = 0; + while (true) { + n++; + list.push_back(data); + auto t1 = std::chrono::high_resolution_clock::now(); + auto delta_t = std::chrono::duration_cast(t1-t0).count(); + //myfile.open("15cpp.csv", std::ios::trunc); + //myfile << n << ", " << delta_t << "\n"; + //myfile.close(); + std::cout << n << ", " << delta_t << "\n"; + } +} diff --git a/lab_2/15.py b/lab_2/15.py new file mode 100644 index 0000000..38daa50 --- /dev/null +++ b/lab_2/15.py @@ -0,0 +1,11 @@ +import time + +start = time.time() +f = open('15py.csv', 'w') +n = 0 +list = [] +while True: + list.append(1024) + n += 1 + end = time.time() + f.write(f'{n}, {end-start}') \ No newline at end of file