From d6ad94de60444adcde952a1f132b4bf42c7c5ee0 Mon Sep 17 00:00:00 2001
From: giuliaghirardini <giuliaghirardini2001@gmail.com>
Date: Mon, 11 Mar 2024 15:41:55 +0100
Subject: [PATCH] [benchmark][speed] Implemented new function read&write.
 Updated Plot and Config

---
 tests/Test.m                      |  2 +-
 tests/dataSpeed.mat               |  4 ++--
 tests/operations/readSpeed.m      |  2 +-
 tests/operations/readWriteSpeed.m | 36 +++++++++++++++++++++++++++++++
 tests/speedBenchmarkConfig.m      | 12 +++++++++++
 tests/speedBenchmarkPlot.m        |  3 +++
 tests/speedPlots.m                | 21 ++++++++++++------
 7 files changed, 70 insertions(+), 10 deletions(-)
 create mode 100644 tests/operations/readWriteSpeed.m

diff --git a/tests/Test.m b/tests/Test.m
index b9164bd..b39e3f6 100644
--- a/tests/Test.m
+++ b/tests/Test.m
@@ -8,6 +8,7 @@ classdef Test < Config
         c   double
         rows double  % Number of rows for random matrix
         cols double  % Number of columns for random matrix
+        e double 
     end
 
     properties(SetAccess = protected)
@@ -15,7 +16,6 @@ classdef Test < Config
     end
 
     properties(Dependent)
-        e
         randomMatrix
         randomVect
     end
diff --git a/tests/dataSpeed.mat b/tests/dataSpeed.mat
index 6a95c21..0eb49fd 100644
--- a/tests/dataSpeed.mat
+++ b/tests/dataSpeed.mat
@@ -1,3 +1,3 @@
 version https://git-lfs.github.com/spec/v1
-oid sha256:a86db703588872061544d2a222cb2e02984b0341a11a5637b7371d0592a471f9
-size 26742
+oid sha256:66a0de3e874d8c280bdea780291cd24128be24983af59683ff246bf2c98ba946
+size 29105
diff --git a/tests/operations/readSpeed.m b/tests/operations/readSpeed.m
index 71d00cc..77e5425 100644
--- a/tests/operations/readSpeed.m
+++ b/tests/operations/readSpeed.m
@@ -1,6 +1,6 @@
 function readTime = readSpeed(len)
     testClass = Test(1, 2, 3, 4, len, len);
-    testStruct = struct;
+    testStruct = struct(testClass);
     
     testStruct.a = 1;
     testStruct.b = 2;
diff --git a/tests/operations/readWriteSpeed.m b/tests/operations/readWriteSpeed.m
new file mode 100644
index 0000000..f7773de
--- /dev/null
+++ b/tests/operations/readWriteSpeed.m
@@ -0,0 +1,36 @@
+function readWriteTime = readWriteSpeed(times, dim)
+% read and write function for assigned matrix 
+    testClass = Test(1, 2, 3, 4, dim, dim);
+    testStruct = struct(testClass);
+    
+    testStruct.a = 1;
+    testStruct.b = 2;
+    testStruct.c = 3;
+   
+    arr1 = zeros(1, dim);
+    arr2 = zeros(1, dim);
+    
+    fields = fieldnames(testClass);
+    
+    tic 
+    for k = 1:length(times)
+        for j = 1:times(k)
+            for i = 1:dim
+                arr1(i) = testStruct.(fields{mod(i,3) + 1});
+                testStruct.e = arr1(i);
+            end
+        end
+    end
+    readWriteTime.struct = toc;
+
+    tic 
+    for k = 1:length(times)
+        for j = 1:times(k)
+            for i = 1:dim
+                arr2(i) = testClass.(fields{mod(i,3) + 1});
+                testClass.e = arr2(i);
+            end
+        end
+    end
+    readWriteTime.class = toc;
+end
\ No newline at end of file
diff --git a/tests/speedBenchmarkConfig.m b/tests/speedBenchmarkConfig.m
index 7955716..dced1b2 100644
--- a/tests/speedBenchmarkConfig.m
+++ b/tests/speedBenchmarkConfig.m
@@ -16,6 +16,18 @@ for i = 1:length(vct)
 end
 clear temp
 
+%% Reading and write few times time
+rw.readWriteTime = [];
+rw.readWriteRatio = [];
+dim = 5;
+
+for i = 1:length(vct)
+    temp = readWriteSpeed(vct(i), dim);
+    rw.readWriteTime = [rw.readWriteTime temp]; %#ok<*AGROW>
+    rw.readWriteRatio = [rw.readWriteRatio temp.class/temp.struct]; 
+end
+clear temp
+
 %% Assignment time
 assignment.assignmentTime = [];
 assignment.assignmentRatio.elementWise = [];
diff --git a/tests/speedBenchmarkPlot.m b/tests/speedBenchmarkPlot.m
index f953975..4cff712 100644
--- a/tests/speedBenchmarkPlot.m
+++ b/tests/speedBenchmarkPlot.m
@@ -7,6 +7,9 @@ load('dataSpeed.mat')
 % Reading time
 [~, ~] = speedPlots(read.readTime, read.readRatio, vct, 'Reading');
 
+%% Read/write time
+[~, ~] = speedPlots(rw.readWriteTime, rw.readWriteRatio, vct, 'Read/write');
+
 %% Assignment time standard
 [~, ~] = speedPlots(assignment.assignmentTime, assignment.assignmentRatio, vct, 'Assignment standard');
 
diff --git a/tests/speedPlots.m b/tests/speedPlots.m
index 3e37e36..bd67118 100644
--- a/tests/speedPlots.m
+++ b/tests/speedPlots.m
@@ -1,9 +1,12 @@
 function [pltConfig, plt] = speedPlots(obj1, obj2, vct, name)
     pltConfig.stdBlue = [0 0.447 0.741];
     pltConfig.stdRed = [0.8500 0.3250 0.0980];
+    speedPlotsFig = figure;
+    tabgp   = uitabgroup(speedPlotsFig);
 
     if strcmp(name,'Assignment element wise')
-        figure
+        tab    = uitab(tabgp,'Title','Time');
+        axes('parent', tab)
         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)
@@ -17,7 +20,8 @@ function [pltConfig, plt] = speedPlots(obj1, obj2, vct, name)
         legend([pltConfig.lgdStruct, pltConfig.lgdClass], {'Struct', 'Class'})
         grid on
         
-        figure 
+        tab    = uitab(tabgp,'Title','Ratio');
+        axes('parent', tab) 
         x = 10:0.1:1000;
         plt.readPlot = semilogx(vct, obj2.elementWise);
         hold on
@@ -29,7 +33,8 @@ function [pltConfig, plt] = speedPlots(obj1, obj2, vct, name)
         grid on
     else    
         if strcmp(name,'Assignment standard')
-            figure
+            tab    = uitab(tabgp,'Title','Time');
+            axes('parent', tab)
             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)
@@ -43,7 +48,8 @@ function [pltConfig, plt] = speedPlots(obj1, obj2, vct, name)
             legend([pltConfig.lgdStruct, pltConfig.lgdClass], {'Struct', 'Class'})
             grid on
             
-            figure 
+            tab    = uitab(tabgp,'Title','Ratio');
+            axes('parent', tab)  
             x = 10:0.1:1000;
             plt.readPlot = semilogx(vct, obj2.std);
             hold on
@@ -54,7 +60,8 @@ function [pltConfig, plt] = speedPlots(obj1, obj2, vct, name)
             legend([plt.readPlot, plt.regrPlot], {'Ratio','Regression'})
             grid on
         else  
-            figure
+            tab    = uitab(tabgp,'Title','Time');
+            axes('parent', tab)
             for i = 1:length(vct)
                 semilogx(vct(i), obj1(i).struct, '.' ,'Color', pltConfig.stdBlue)
                 semilogx(vct(i), obj1(i).class, '.', 'Color', pltConfig.stdRed)
@@ -68,7 +75,8 @@ function [pltConfig, plt] = speedPlots(obj1, obj2, vct, name)
             legend([pltConfig.lgdStruct, pltConfig.lgdClass], {'Struct', 'Class'})
             grid on
             
-            figure 
+            tab    = uitab(tabgp,'Title','Ratio');
+            axes('parent', tab) 
             x = 10:0.1:1000;
             plt.readPlot = semilogx(vct, obj2);
             hold on
@@ -78,6 +86,7 @@ function [pltConfig, plt] = speedPlots(obj1, obj2, vct, name)
             ylabel('Ratio')
             legend([plt.readPlot, plt.regrPlot], {'Ratio','Regression'})
             grid on
+            
         end
     end
 end
\ No newline at end of file
-- 
GitLab