1
1
#include " PcaTest.h"
2
2
3
3
// #include <Util/ProgressMonitHigh.hpp>
4
+ #include < Ios/Osstream.hpp>
5
+ #include < Ios/Ofstream.hpp>
4
6
#include < Util/CoutBuf.hpp>
5
7
#include < Util/VecD.hpp>
6
8
#include < Util/Timer.hpp>
@@ -20,26 +22,43 @@ PcaTest::PcaTest()
20
22
{
21
23
const Timer timer;
22
24
LOGL << " Running performance tests" << Nl;
25
+ TestMatrixTransp ();
26
+ TestMatrixMul ();
23
27
TestDistrib ();
28
+ // return;
24
29
TestPca1 ();
30
+ TestPcaAuto ();
31
+
32
+
25
33
Log (" Total" , timer, 0 );
26
34
}
27
35
28
36
PcaTest::~PcaTest (){}
29
37
30
38
void PcaTest::Log (const EnjoLib::Str & descr, const EnjoLib::Timer & timer, const int iter) const
31
39
{
32
- LOGL << descr <<" \t (" << iter << " iters)\t " << " finished in: " << timer.GetElapsedSeconds () << " s\n " ;
40
+ #ifdef EL_FLOATING_POINT_LOW_PRECISION
41
+ const Str fpName = " float" ;
42
+ #else
43
+ const Str fpName = " double" ;
44
+ #endif // EL_FLOATING_POINT_LOW_PRECISION
45
+ Osstream oss;
46
+ oss << descr <<" \t (" << iter << " iters)\t " << " finished in: " << timer.GetElapsedSeconds () << " s\n " ;
47
+ const Str fname = " perf-test-" + fpName + " .log" ;
48
+ Ofstream ofs (fname, true , true );
49
+ ofs << oss.str ();
50
+ LOGL << oss.str ();
51
+ // {LOGL << "Saved log to " << fname << Nl;}
33
52
}
34
53
35
54
void PcaTest::TestDistrib () const
36
55
{
37
- const int ITER = 4e3 ;
38
- const Timer timer;
56
+ const int ITER = 3e3 ;
39
57
const RandomMath rmath;
40
58
const Distrib distr;
41
59
42
60
VecD data;
61
+ const Timer timer;
43
62
for (int i = 0 ; i < ITER; ++i)
44
63
{
45
64
// break;
@@ -54,16 +73,32 @@ void PcaTest::TestDistrib() const
54
73
const DistribData & distData = distr.GetDistrib (data, 50 );
55
74
}
56
75
57
- Log (" Distrib" , timer, ITER);
76
+ Log (" Distrib\t " , timer, ITER);
58
77
}
59
78
60
79
void PcaTest::TestPca1 () const
61
80
{
62
- const int ITER = 6e3 ;
81
+ const int ITER = 1e4 ;
82
+ const int numFeaturesToLeave = 3 ;
83
+ const Matrix & iris = DataSets::GetIris ();
84
+ const auto eig = EigenFactory ().Create (EIGENTYPE_DEFAULT);
63
85
const Timer timer;
86
+ for (int i = 0 ; i < ITER; ++i)
87
+ {
88
+ const PCA pca (*eig, iris, numFeaturesToLeave);
89
+ const Matrix & xtr = pca.Transform (iris);
90
+ const Matrix & xinv = pca.InverseTransform (xtr);
91
+ }
92
+ Log (" PCA_1\t " , timer, ITER);
93
+ }
94
+
95
+ void PcaTest::TestPcaAuto () const
96
+ {
97
+ const int ITER = 4e3 ;
64
98
const Matrix & data = DataSets::GetIris ();
65
99
const auto eig = EigenFactory ().Create (EIGENTYPE_DEFAULT);
66
100
const PCAAuto pcaAuto;
101
+ const Timer timer;
67
102
68
103
for (int i = 0 ; i < ITER; ++i)
69
104
{
@@ -73,5 +108,46 @@ void PcaTest::TestPca1() const
73
108
const int minComp1 = pcaAuto.FindMinimalComponents (*eig, data, quality);
74
109
const int minComp2 = pcaAuto.FindMinimalComponentsBinSearch (*eig, data, quality);
75
110
}
76
- Log (" PCA1" , timer, ITER);
111
+ Log (" PCA_auto" , timer, ITER);
112
+ }
113
+
114
+ void PcaTest::TestMatrixMul () const
115
+ {
116
+ Matrix matData;
117
+ matData.push_back ({1 , 2 , 3 });
118
+ matData.push_back ({1 , 2 , 3 });
119
+ matData.push_back ({1 , 2 , 3 });
120
+ matData.push_back ({1 , 2 , 3 });
121
+ matData.push_back ({1 , 2 , 3 });
122
+ matData.push_back ({1 , 2 , 3 });
123
+ matData.push_back ({1 , 2 , 3 });
124
+
125
+ const Matrix & matDataT = matData.T ();
126
+ const Timer timer;
127
+ const int ITER = 2e5 ;
128
+ for (int i = 0 ; i < ITER; ++i)
129
+ {
130
+ const Matrix & matMul = matDataT * matData;
131
+ }
132
+ Log (" Matrix mul" , timer, ITER);
133
+ }
134
+
135
+ void PcaTest::TestMatrixTransp () const
136
+ {
137
+ Matrix matData;
138
+ matData.push_back ({1 , 2 , 3 });
139
+ matData.push_back ({1 , 2 , 3 });
140
+ matData.push_back ({1 , 2 , 3 });
141
+ matData.push_back ({1 , 2 , 3 });
142
+ matData.push_back ({1 , 2 , 3 });
143
+ matData.push_back ({1 , 2 , 3 });
144
+ matData.push_back ({1 , 2 , 3 });
145
+
146
+ const Timer timer;
147
+ const int ITER = 4e5 ;
148
+ for (int i = 0 ; i < ITER; ++i)
149
+ {
150
+ const Matrix & matT = matData.T ();
151
+ }
152
+ Log (" Matrix Trans" , timer, ITER);
77
153
}
0 commit comments