-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbenchPVector.cpp
88 lines (70 loc) · 2.43 KB
/
benchPVector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
///////////////// benchPVector.cpp ///////////////////
//
// Compare the times of pvector<> and std::vector<>.
// The tests perform certain ammount of std::sort()
// on vectors.
//
// Author: Radoslav Getov
//
//////////////////////////////////////////////////////
#include "pvector.h" // for pvector<>
#include <iostream> // for cout, etc
#include <iomanip> // for endl
#include <algorithm> // for generate(), sort()
#include <time.h> // for clock()
using namespace std;
const size_t SIZE = 1000; // of vectors
const int LOOPS = 5000; // in the test
// -------------- DO_MEASURE --------
#define DO_MEASURE \
\
srand (0); \
clock_t start = clock(); \
for (int j = 0; j < LOOPS; j++) \
{ \
std::generate (p.begin(), p.end(), rand); \
std::sort (p.begin(), p.end()); \
} \
return (double)(clock() - start) / CLOCKS_PER_SEC;
// --------- measure pvector <int> ----------
static double measurePvectorI()
{
pvector<int> p (SIZE);
DO_MEASURE
}
// --------- measure vector <int> -----------
static double measureVectorI()
{
std::vector<int> p (SIZE);
DO_MEASURE
}
// --------- measure pvector <double> -------
static double measurePvectorD()
{
pvector<double> p (SIZE);
DO_MEASURE
}
// --------- measure vector <double> -------
static double measureVectorD()
{
std::vector<double> p (SIZE);
DO_MEASURE
}
// -------- compare times --------
void benchPVector()
{
double t_v_i = measureVectorI (),
t_p_i = measurePvectorI (),
ratio_i = t_p_i / t_v_i;
cout << "Times: pvector <int> : " << t_p_i << endl
<< " vector <int> : " << t_v_i << endl
<< " ratio : " << ratio_i << endl
<< endl;
double t_p_d = measurePvectorD (),
t_v_d = measureVectorD (),
ratio_d = t_p_d / t_v_d;
cout << " pvector <double> : " << t_p_d << endl
<< " vector <double> : " << t_v_d << endl
<< " ratio : " << ratio_d << endl
<< endl;
}