"Ferdig" med øving 2

This commit is contained in:
Trygve 2024-03-05 10:35:30 +01:00
parent 42176e4c76
commit 7b40a4243d
7 changed files with 74 additions and 4 deletions

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -1,6 +1,7 @@
#include <cassert>
#include <cstdlib>
#include <ctime>
#include <fstream>
#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;
}

View File

@ -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()

23
lab_2/15.cpp Normal file
View File

@ -0,0 +1,23 @@
#include <vector>
#include <chrono>
#include <fstream>
#include <iostream>
int main()
{
auto t0 = std::chrono::high_resolution_clock::now();
//std::ofstream myfile;
std::vector<int> 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<std::chrono::microseconds>(t1-t0).count();
//myfile.open("15cpp.csv", std::ios::trunc);
//myfile << n << ", " << delta_t << "\n";
//myfile.close();
std::cout << n << ", " << delta_t << "\n";
}
}

11
lab_2/15.py Normal file
View File

@ -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}')