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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| //g++ -std=c++11 -O3 -DCHRONO -o foo foo.cpp && ./foo 2> /dev/null
#include <iostream>
#include <ctime>
#include <memory>
#include <cstring>
const int size = 5000000;
const int nb_tests = 100;
#ifdef CHRONO
# include <chrono>
# define RESET duration = 0;
# define START t0 = clock::now();
# define END duration += duration_cast<time_type>(clock::now() - t0).count();
#else
# define WIN32_LEAN_AND_MEAN
# include <Windows.h>
# define RESET duration = 0.0;
# define START QueryPerformanceCounter(&begin);
# define END QueryPerformanceCounter(&end); duration += (end.QuadPart - begin.QuadPart) / freq;
#endif // CHRONO
class TableauIntRef {
private:
int* tableau;
int taille;
public :
explicit TableauIntRef(): taille(0), tableau(nullptr) { }
explicit TableauIntRef(int t): taille(t), tableau(new int[t]) { }
explicit TableauIntRef(const TableauIntRef& t): taille(t.taille), tableau(new int[t.taille]) {
memcpy(tableau, t.tableau, sizeof(int) * taille);
}
~TableauIntRef() { delete tableau; }
int getElement(int i) const { return tableau[i]; }
void setElement(int i, int elt) { tableau[i] = elt; }
int getTaille() const { return taille; }
void swap(TableauIntRef& t) {
std::swap(tableau, t.tableau);
std::swap(taille, t.taille);
}
TableauIntRef& operator=(const TableauIntRef& t) {
TableauIntRef cp(t);
swap(cp);
return *this;
}
};
class TableauIntCpy {
private:
int* tableau;
int taille;
public :
explicit TableauIntCpy(): taille(0), tableau(nullptr) { }
explicit TableauIntCpy(int t): taille(t), tableau(new int[t]) { }
TableauIntCpy(const TableauIntCpy& t): taille(t.taille), tableau(new int[t.taille]) {
memcpy(tableau, t.tableau, sizeof(int) * taille);
}
~TableauIntCpy() { delete tableau; }
int getElement(int i) const { return tableau[i]; }
void setElement(int i, int elt) { tableau[i] = elt; }
int getTaille() const { return taille; }
void swap(TableauIntCpy& t) {
std::swap(tableau, t.tableau);
std::swap(taille, t.taille);
}
TableauIntCpy& operator=(TableauIntCpy cp) {
swap(cp);
return *this;
}
};
template <class T>
void useData(T& t0, T& t1) {
int a=0, b=0;
for(int i=0; i<size; ++i) {
t0.setElement(i, rand());
}
for(int i=0; i<size; ++i) {
a += t0.getElement(i);
b += t1.getElement(i);
}
std::cerr << a << b << std::endl;
}
int main(int argc, char **argv) {
#ifdef CHRONO
typedef std::chrono::high_resolution_clock clock;
typedef clock::time_point time_point;
typedef std::chrono::microseconds time_type;
using std::chrono::duration_cast;
time_point t0;
long long duration;
#else
LARGE_INTEGER qfreq, begin, end;
QueryPerformanceFrequency(&qfreq);
double freq = qfreq.QuadPart / (double) 1000000; // µs
double duration;
#endif // CHRONO
TableauIntRef r0(size), r1;
TableauIntCpy c0(size), c1;
srand(time(0));
for(int i=0; i<size; ++i) {
r0.setElement(i, rand());
c0.setElement(i, rand());
}
RESET;
for(int i=0; i<nb_tests; ++i) {
START;
r1 = r0;
END;
// utiliser les données pour pas que le compilo vire le code
useData(r0, r1);
}
std::cout << duration / (double)nb_tests << "us (ref)" << std::endl;
RESET;
for(int i=0; i<nb_tests; ++i) {
START;
c1 = c0;
END;
// utiliser les données pour pas que le compilo vire le code
useData(c0, c1);
}
std::cout << duration / (double)nb_tests << "us (copy)" << std::endl;
return 0;
} |