From 828cb4e72a7d8e952265499f023ed6030779dd2e Mon Sep 17 00:00:00 2001
From: giuliaghirardini <giuliaghirardini2001@gmail.com>
Date: Mon, 11 Mar 2024 11:53:27 +0100
Subject: [PATCH] [benchmark][speed] Created speedPlots function and modified
 Config and Plots implementations

---
 tests/speedBenchmark.m       |  92 --------------------
 tests/speedBenchmarkConfig.m | 114 +++++++++++++++++++++++++
 tests/speedBenchmarkPlot.m   | 159 +++--------------------------------
 tests/speedPlots.m           |  83 ++++++++++++++++++
 4 files changed, 209 insertions(+), 239 deletions(-)
 delete mode 100644 tests/speedBenchmark.m
 create mode 100644 tests/speedBenchmarkConfig.m
 create mode 100644 tests/speedPlots.m

diff --git a/tests/speedBenchmark.m b/tests/speedBenchmark.m
deleted file mode 100644
index 4779e1c..0000000
--- a/tests/speedBenchmark.m
+++ /dev/null
@@ -1,92 +0,0 @@
-%% 
-clc; close all; clear all;
-warning off 
-% call of a warning because converting class to a struct in function
-
-% TOFIX:  assignment
-vct = 10:10:10^3;
-%% Functions
-
-%%% Reading time
-readTime = [];
-for i = 1:length(vct)
-    head = readSpeed(vct(i));
-    readTime = [readTime head]; %#ok<*AGROW>
-end
-clear head
-
-%% Assignment time
-assignmentTime = [];
-for i = 1:length(vct)
-    head = assignmentSpeed(vct(i));
-    assignmentTime = [assignmentTime head]; %#ok<*AGROW>
-end
-clear head
-
-%% Scalar product time
-scalarProductTime = [];
-for i = 1:length(vct)
-    head = scalarProductSpeed(vct(i));
-    scalarProductTime = [scalarProductTime head]; %#ok<*AGROW>
-end
-clear head
-
-%% Eigen 
-
-eigenTime = [];
-for i = 1:length(vct)
-    head = eigenSpeed(vct(i));
-    eigenTime = [eigenTime head]; %#ok<*AGROW>
-end
-clear head
-
-%% Sum time
-sumTime = [];
-for i = 1:length(vct)
-    head = sumSpeed(vct(i));
-    sumTime = [sumTime head]; %#ok<*AGROW>
-end
-clear head
-
-%% Cross product time
-crossProductTime = [];
-for i = 1:length(vct)
-    head = crossProductSpeed(vct(i));
-    crossProductTime = [crossProductTime head]; %#ok<*AGROW>
-end
-clear head
-
-%% Transpose time
-transposeTime = [];
-for i = 1:length(vct)
-    head = transposeSpeed(vct(i));
-    transposeTime = [transposeTime head]; %#ok<*AGROW>
-end
-clear head
-
-%% Inverse time
-inverseTime = [];
-for i = 1:length(vct)
-    head = inverseSpeed(vct(i));
-    inverseTime = [inverseTime head]; %#ok<*AGROW>
-end
-clear head
-
-%% Determinant time
-determinantTime = [];
-for i = 1:length(vct)
-    head = determinantSpeed(vct(i));
-    determinantTime = [determinantTime head]; %#ok<*AGROW>
-end
-clear head
-
-%% Mldivide (\) time
-mldivideTime = [];
-for i = 1:length(vct)
-    head = mldivideSpeed(vct(i));
-    mldivideTime = [mldivideTime head]; %#ok<*AGROW>
-end
-clear head
-
-%% Saving data
-save('dataSpeed.mat')
\ No newline at end of file
diff --git a/tests/speedBenchmarkConfig.m b/tests/speedBenchmarkConfig.m
new file mode 100644
index 0000000..7955716
--- /dev/null
+++ b/tests/speedBenchmarkConfig.m
@@ -0,0 +1,114 @@
+%% 
+clc; close all; clear all;
+warning off 
+% call of a warning because converting class to a struct in function
+
+vct = 10:10:10^3;
+%% Functions
+
+%%% Reading time
+read.readTime = [];
+read.readRatio = [];
+for i = 1:length(vct)
+    temp = readSpeed(vct(i));
+    read.readTime = [read.readTime temp]; %#ok<*AGROW>
+    read.readRatio = [read.readRatio temp.class/temp.struct]; 
+end
+clear temp
+
+%% Assignment time
+assignment.assignmentTime = [];
+assignment.assignmentRatio.elementWise = [];
+assignment.assignmentRatio.std = [];
+for i = 1:length(vct)
+    temp = assignmentSpeed(vct(i));
+    assignment.assignmentTime = [assignment.assignmentTime temp]; %#ok<*AGROW>
+    assignment.assignmentRatio.elementWise = [assignment.assignmentRatio.elementWise ...
+        temp.elementWise.class/temp.elementWise.struct]; 
+    assignment.assignmentRatio.std = [assignment.assignmentRatio.std ...
+        temp.std.class/temp.std.struct]; 
+end
+clear temp
+
+%% Scalar product time
+scalar.scalarProductTime = [];
+scalar.scalarProductRatio = [];
+for i = 1:length(vct)
+    temp = scalarProductSpeed(vct(i));
+    scalar.scalarProductTime = [scalar.scalarProductTime temp]; %#ok<*AGROW>
+    scalar.scalarProductRatio = [scalar.scalarProductRatio temp.class/temp.struct]; 
+end
+clear temp
+
+%% Eigen 
+eigen.eigenTime = [];
+eigen.eigenRatio = [];
+for i = 1:length(vct)
+    temp = eigenSpeed(vct(i));
+    eigen.eigenTime = [eigen.eigenTime temp]; %#ok<*AGROW>
+    eigen.eigenRatio = [eigen.eigenRatio temp.class/temp.struct];
+end
+clear temp
+
+%% Sum time
+sum.sumTime = [];
+sum.sumRatio = [];
+for i = 1:length(vct)
+    temp = sumSpeed(vct(i));
+    sum.sumTime = [sum.sumTime temp]; %#ok<*AGROW>
+    sum.sumRatio = [sum.sumRatio temp.class/temp.struct]; %#ok<*AGROW>
+end
+clear temp
+
+%% Cross product time
+cross.crossProductTime = [];
+cross.crossProductRatio = [];
+for i = 1:length(vct)
+    temp = crossProductSpeed(vct(i));
+    cross.crossProductTime = [cross.crossProductTime temp]; %#ok<*AGROW>
+    cross.crossProductRatio = [cross.crossProductRatio temp.class/temp.struct];
+end
+clear temp
+
+%% Transpose time
+transpose.transposeTime = [];
+transpose.transposeRatio = [];
+for i = 1:length(vct)
+    temp = transposeSpeed(vct(i));
+    transpose.transposeTime = [transpose.transposeTime temp]; %#ok<*AGROW>
+    transpose.transposeRatio = [transpose.transposeRatio temp.class/temp.struct];
+end
+clear temp
+
+%% Inverse time
+inverse.inverseTime = [];
+inverse.inverseRatio = [];
+for i = 1:length(vct)
+    temp = inverseSpeed(vct(i));
+    inverse.inverseTime = [inverse.inverseTime temp]; %#ok<*AGROW>
+    inverse.inverseRatio = [inverse.inverseRatio temp.class/temp.struct];
+end
+clear temp
+
+%% Determinant time
+det.determinantTime = [];
+det.determinantRatio = [];
+for i = 1:length(vct)
+    temp = determinantSpeed(vct(i));
+    det.determinantTime = [det.determinantTime temp]; %#ok<*AGROW>
+    det.determinantRatio = [det.determinantRatio temp.class/temp.struct];
+end
+clear temp
+
+%% Mldivide (\) time
+mldiv.mldivideTime = [];
+mldiv.mldivideRatio = [];
+for i = 1:length(vct)
+    temp = mldivideSpeed(vct(i));
+    mldiv.mldivideTime = [mldiv.mldivideTime temp]; %#ok<*AGROW>
+    mldiv.mldivideRatio = [mldiv.mldivideRatio temp.class/temp.struct];
+end
+clear temp
+
+%% Saving data
+save('dataSpeed.mat')
\ No newline at end of file
diff --git a/tests/speedBenchmarkPlot.m b/tests/speedBenchmarkPlot.m
index cb8c5c3..f953975 100644
--- a/tests/speedBenchmarkPlot.m
+++ b/tests/speedBenchmarkPlot.m
@@ -1,173 +1,38 @@
 % Calling speedBenchmark script
-% speedBenchmark;
 clc; close all; clear all;
 load('dataSpeed.mat')
 
 %% Plots
-plt.stdBlue = [0 0.447 0.741];
-plt.stdRed = [0.8500 0.3250 0.0980];
 
 % Reading time
-figure
-for i = 1:length(vct)
-    semilogx(vct(i), readTime(i).struct, '.' ,'Color', plt.stdBlue)
-    semilogx(vct(i), readTime(i).class, '.', 'Color', plt.stdRed)
-    hold on
-end
-plt.lgdStruct = plot(nan, nan, 'color', plt.stdBlue);
-plt.lgdClass = plot(nan, nan, 'color', plt.stdRed);
-title('Reading time');
-xlabel('Number of elements')
-ylabel('Speed [s]')
-legend([plt.lgdStruct, plt.lgdClass], {'Struct', 'Class'})
-grid on
+[~, ~] = speedPlots(read.readTime, read.readRatio, vct, 'Reading');
 
 %% Assignment time standard
-figure
-for i = 1:length(vct)
-    semilogx(vct(i), assignmentTime(i).std.struct, '.' ,'Color', plt.stdBlue)
-    semilogx(vct(i), assignmentTime(i).std.class, '.', 'Color', plt.stdRed)
-    hold on
-end
-plt.lgdStruct = plot(nan, nan, 'color', plt.stdBlue);
-plt.lgdClass = plot(nan, nan, 'color', plt.stdRed);
-title('Assignment time standard');
-xlabel('Number of elements')
-ylabel('Speed [s]')
-legend([plt.lgdStruct, plt.lgdClass], {'Struct', 'Class'})
-grid on
+[~, ~] = speedPlots(assignment.assignmentTime, assignment.assignmentRatio, vct, 'Assignment standard');
 
-% Assignment time element wise
-figure
-for i = 1:length(vct)
-    semilogx(vct(i), assignmentTime(i).elementWise.struct, '.' ,'Color', plt.stdBlue)
-    semilogx(vct(i), assignmentTime(i).elementWise.class, '.', 'Color', plt.stdRed)
-    hold on
-end
-plt.lgdStruct = plot(nan, nan, 'color', plt.stdBlue);
-plt.lgdClass = plot(nan, nan, 'color', plt.stdRed);
-title('Assignment time element wise');
-xlabel('Number of elements')
-ylabel('Speed [s]')
-legend([plt.lgdStruct, plt.lgdClass], {'Struct', 'Class'})
-grid on
+%% Assignment time element wise
+[~, ~] = speedPlots(assignment.assignmentTime, assignment.assignmentRatio, vct, 'Assignment element wise');
 
 %% Scalar product
-figure
-for i = 1:length(vct)
-    semilogx(vct(i), scalarProductTime(i).struct, '.' ,'Color', plt.stdBlue)
-    semilogx(vct(i), scalarProductTime(i).class, '.', 'Color', plt.stdRed)
-    hold on
-end
-plt.lgdStruct = plot(nan, nan, 'color', plt.stdBlue);
-plt.lgdClass = plot(nan, nan, 'color', plt.stdRed);
-title('Scalar product time');
-xlabel('Number of elements')
-ylabel('Speed [s]')
-legend([plt.lgdStruct, plt.lgdClass], {'Struct', 'Class'})
-grid on
+[~, ~] = speedPlots(scalar.scalarProductTime, scalar.scalarProductRatio, vct, 'Scalar product');
 
 %% Sum 
-figure
-for i = 1:length(vct)
-    semilogx(vct(i), sumTime(i).struct, '.' ,'Color', plt.stdBlue)
-    semilogx(vct(i), sumTime(i).class, '.', 'Color', plt.stdRed)
-    hold on
-end
-plt.lgdStruct = plot(nan, nan, 'color', plt.stdBlue);
-plt.lgdClass = plot(nan, nan, 'color', plt.stdRed);
-title('Sum time');
-xlabel('Number of elements')
-ylabel('Speed [s]')
-legend([plt.lgdStruct, plt.lgdClass], {'Struct', 'Class'})
-grid on
+[~, ~] = speedPlots(sum.sumTime, sum.sumRatio, vct, 'Sum');
 
 %% Cross product time (matrices) 
-figure
-for i = 1:length(vct)
-    semilogx(vct(i), crossProductTime(i).struct, '.' ,'Color', plt.stdBlue)
-    semilogx(vct(i), crossProductTime(i).class, '.', 'Color', plt.stdRed)
-    hold on
-end
-plt.lgdStruct = plot(nan, nan, 'color', plt.stdBlue);
-plt.lgdClass = plot(nan, nan, 'color', plt.stdRed);
-title('Cross product time (matrices)');
-xlabel('Number of elements')
-ylabel('Speed [s]')
-legend([plt.lgdStruct, plt.lgdClass], {'Struct', 'Class'})
-grid on
+[~, ~] = speedPlots(cross.crossProductTime, cross.crossProductRatio, vct, 'Cross product (matrices)');
 
 %% Eigenvalues problem time (matrices) 
-figure
-for i = 1:length(vct)
-    semilogx(vct(i), eigenTime(i).struct, '.' ,'Color', plt.stdBlue)
-    semilogx(vct(i), eigenTime(i).class, '.', 'Color', plt.stdRed)
-    hold on
-end
-plt.lgdStruct = plot(nan, nan, 'color', plt.stdBlue);
-plt.lgdClass = plot(nan, nan, 'color', plt.stdRed);
-title('Eigenvalues problem time (matrices) ');
-xlabel('Number of elements')
-ylabel('Speed [s]')
-legend([plt.lgdStruct, plt.lgdClass], {'Struct', 'Class'})
-grid on
+[~, ~] = speedPlots(eigen.eigenTime, eigen.eigenRatio, vct, 'Eigenvalue problem (matrices)');
 
 %% Transpose time (matrices) 
-figure
-for i = 1:length(vct)
-    semilogx(vct(i), transposeTime(i).struct, '.' ,'Color', plt.stdBlue)
-    semilogx(vct(i), transposeTime(i).class, '.', 'Color', plt.stdRed)
-    hold on
-end
-plt.lgdStruct = plot(nan, nan, 'color', plt.stdBlue);
-plt.lgdClass = plot(nan, nan, 'color', plt.stdRed);
-title('Transpose time (matrices)');
-xlabel('Number of elements')
-ylabel('Speed [s]')
-legend([plt.lgdStruct, plt.lgdClass], {'Struct', 'Class'})
-grid on
+[~, ~] = speedPlots(transpose.transposeTime, transpose.transposeRatio, vct, 'Transpose (matrices)');
 
 %% Inverse time (matrices) 
-figure
-for i = 1:length(vct)
-    semilogx(vct(i), inverseTime(i).struct, '.' ,'Color', plt.stdBlue)
-    semilogx(vct(i), inverseTime(i).class, '.', 'Color', plt.stdRed)
-    hold on
-end
-plt.lgdStruct = plot(nan, nan, 'color', plt.stdBlue);
-plt.lgdClass = plot(nan, nan, 'color', plt.stdRed);
-title('Inverse time (matrices)');
-xlabel('Number of elements')
-ylabel('Speed [s]')
-legend([plt.lgdStruct, plt.lgdClass], {'Struct', 'Class'})
-grid on
+[~, ~] = speedPlots(inverse.inverseTime, inverse.inverseRatio, vct, 'Inverse (matrices)');
 
 %% Determinant time (matrices) 
-figure
-for i = 1:length(vct)
-    semilogx(vct(i), determinantTime(i).struct, '.' ,'Color', plt.stdBlue)
-    semilogx(vct(i), determinantTime(i).class, '.', 'Color', plt.stdRed)
-    hold on
-end
-plt.lgdStruct = plot(nan, nan, 'color', plt.stdBlue);
-plt.lgdClass = plot(nan, nan, 'color', plt.stdRed);
-title('Determinant time (matrices)');
-xlabel('Number of elements')
-ylabel('Speed [s]')
-legend([plt.lgdStruct, plt.lgdClass], {'Struct', 'Class'})
-grid on
+[~, ~] = speedPlots(det.determinantTime, det.determinantRatio, vct, 'Determinant (matrices)');
 
 %% Mldivide (\) time (matrices) 
-figure
-for i = 1:length(vct)
-    semilogx(vct(i), mldivideTime(i).struct, '.' ,'Color', plt.stdBlue)
-    semilogx(vct(i), mldivideTime(i).class, '.', 'Color', plt.stdRed)
-    hold on
-end
-plt.lgdStruct = plot(nan, nan, 'color', plt.stdBlue);
-plt.lgdClass = plot(nan, nan, 'color', plt.stdRed);
-title('Mldivide time (matrices)');
-xlabel('Number of elements')
-ylabel('Speed [s]')
-legend([plt.lgdStruct, plt.lgdClass], {'Struct', 'Class'})
-grid on
\ No newline at end of file
+[~, ~] = speedPlots(mldiv.mldivideTime, mldiv.mldivideRatio, vct, '\ (matrices)');
\ No newline at end of file
diff --git a/tests/speedPlots.m b/tests/speedPlots.m
new file mode 100644
index 0000000..3e37e36
--- /dev/null
+++ b/tests/speedPlots.m
@@ -0,0 +1,83 @@
+function [pltConfig, plt] = speedPlots(obj1, obj2, vct, name)
+    pltConfig.stdBlue = [0 0.447 0.741];
+    pltConfig.stdRed = [0.8500 0.3250 0.0980];
+
+    if strcmp(name,'Assignment element wise')
+        figure
+        for i = 1:length(vct)
+            semilogx(vct(i), obj1(i).elementWise.struct, '.' ,'Color', pltConfig.stdBlue)
+            semilogx(vct(i), obj1(i).elementWise.class, '.', 'Color', pltConfig.stdRed)
+            hold on
+        end
+        pltConfig.lgdStruct = plot(nan, nan, 'color', pltConfig.stdBlue);
+        pltConfig.lgdClass = plot(nan, nan, 'color', pltConfig.stdRed);
+        title(strcat(name,' time'));
+        xlabel('Number of elements')
+        ylabel('Speed [s]')
+        legend([pltConfig.lgdStruct, pltConfig.lgdClass], {'Struct', 'Class'})
+        grid on
+        
+        figure 
+        x = 10:0.1:1000;
+        plt.readPlot = semilogx(vct, obj2.elementWise);
+        hold on
+        plt.regrPlot = plot(x,polyval(polyfit(vct, obj2.elementWise, 10),x), 'k--');
+        title(strcat(name,' time ratio'));
+        xlabel('Number of elements')
+        ylabel('Ratio')
+        legend([plt.readPlot, plt.regrPlot], {'Ratio','Regression'})
+        grid on
+    else    
+        if strcmp(name,'Assignment standard')
+            figure
+            for i = 1:length(vct)
+                semilogx(vct(i), obj1(i).std.struct, '.' ,'Color', pltConfig.stdBlue)
+                semilogx(vct(i), obj1(i).std.class, '.', 'Color', pltConfig.stdRed)
+                hold on
+            end
+            pltConfig.lgdStruct = plot(nan, nan, 'color', pltConfig.stdBlue);
+            pltConfig.lgdClass = plot(nan, nan, 'color', pltConfig.stdRed);
+            title(strcat(name,' time'));
+            xlabel('Number of elements')
+            ylabel('Speed [s]')
+            legend([pltConfig.lgdStruct, pltConfig.lgdClass], {'Struct', 'Class'})
+            grid on
+            
+            figure 
+            x = 10:0.1:1000;
+            plt.readPlot = semilogx(vct, obj2.std);
+            hold on
+            plt.regrPlot = plot(x,polyval(polyfit(vct, obj2.std, 10),x), 'k--');
+            title(strcat(name,' time ratio'));
+            xlabel('Number of elements')
+            ylabel('Ratio')
+            legend([plt.readPlot, plt.regrPlot], {'Ratio','Regression'})
+            grid on
+        else  
+            figure
+            for i = 1:length(vct)
+                semilogx(vct(i), obj1(i).struct, '.' ,'Color', pltConfig.stdBlue)
+                semilogx(vct(i), obj1(i).class, '.', 'Color', pltConfig.stdRed)
+                hold on
+            end
+            pltConfig.lgdStruct = plot(nan, nan, 'color', pltConfig.stdBlue);
+            pltConfig.lgdClass = plot(nan, nan, 'color', pltConfig.stdRed);
+            title(strcat(name,' time'));
+            xlabel('Number of elements')
+            ylabel('Speed [s]')
+            legend([pltConfig.lgdStruct, pltConfig.lgdClass], {'Struct', 'Class'})
+            grid on
+            
+            figure 
+            x = 10:0.1:1000;
+            plt.readPlot = semilogx(vct, obj2);
+            hold on
+            plt.regrPlot = plot(x,polyval(polyfit(vct, obj2, 10),x), 'k--');
+            title(strcat(name,' time ratio'));
+            xlabel('Number of elements')
+            ylabel('Ratio')
+            legend([plt.readPlot, plt.regrPlot], {'Ratio','Regression'})
+            grid on
+        end
+    end
+end
\ No newline at end of file
-- 
GitLab