My favorites | Sign in
Project Home Wiki Issues Source
Checkout   Browse   Changes  
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* File: main.cpp
* Author: peter
*
* Created on 04 August 2011, 14:26
*/

#include <cstdlib>
#include <iostream>
#include <time.h>
#include <sys/time.h>

using namespace std;

#define length (64LL * 1024 * 1024 * 1024)

double diff(timeval a, timeval b) {
return a.tv_sec - b.tv_sec + (a.tv_usec - b.tv_usec) / 1e6;
}

/*
*
*/
void timeBytesTest() {

struct timeval init, mid1, mid2, final;
gettimeofday(&init, 0);

//
char* bytes = new char[length];

gettimeofday(&mid1, 0);
for (long long i = 0; i < length; i++)
bytes[i] = (char) i;

gettimeofday(&mid2, 0);

for (long long i = 0; i < length; i++) {
char b = bytes[i];
if (b != (char) i) {
cerr << "Expected " << (char) i << " but was " << b << endl;
return;
}
}
gettimeofday(&final, 0);
delete bytes;

//
cout << "char[] took "
<< diff(mid1, init)*1e6 << " usec to allocate and "
<< diff(mid2, mid1) << " sec to write and "
<< diff(final, mid2) << " sec to read" << endl;
return;
}

void timeBytesUnrolledTest() {

struct timeval init, mid1, mid2, final;
gettimeofday(&init, 0);

//
char* bytes = new char[length];

gettimeofday(&mid1, 0);
for (long long i = 0; i < length; i += 4) {
bytes[i] = (char) i;
bytes[i + 1] = (char) (i + 1);
bytes[i + 2] = (char) (i + 2);
bytes[i + 3] = (char) (i + 3);
}

gettimeofday(&mid2, 0);

for (long long i = 0; i < length; i += 4) {
char b0 = bytes[i];
char b1 = bytes[i + 1];
char b2 = bytes[i + 2];
char b3 = bytes[i + 3];
if (b0 != (char) i) {
cerr << "Expected " << (char) i << " but was " << b0 << endl;
return;
}
if (b1 != (char) (i + 1)) {
cerr << "Expected " << (char) (i + 1) << " but was " << b1 << endl;
return;
}
if (b2 != (char) (i + 2)) {
cerr << "Expected " << (char) (i + 2) << " but was " << b2 << endl;
return;
}
if (b3 != (char) (i + 3)) {
cerr << "Expected " << (char) (i + 3) << " but was " << b3 << endl;
return;
}
}
gettimeofday(&final, 0);
delete bytes;

//
cout << "char[] unrolled took "
<< diff(mid1, init)*1e6 << " usec to allocate and "
<< diff(mid2, mid1) << " sec to write and "
<< diff(final, mid2) << " sec to read" << endl;
return;
}

/*
*
*/
void timeLongTest() {

struct timeval init, mid1, mid2, final;
gettimeofday(&init, 0);

//
long long* longs = new long long[length/8];

gettimeofday(&mid1, 0);
for (long long i = 0; i < length/8; i++)
longs[i] = i;

gettimeofday(&mid2, 0);

for (long long i = 0; i < length/8; i++) {
long long l = longs[i];
if (l != i) {
cerr << "Expected " << i << " but was " << l << endl;
return;
}
}
gettimeofday(&final, 0);
delete longs;

//
cout << "long[] took "
<< diff(mid1, init)*1e6 << " usec to allocate and "
<< diff(mid2, mid1) << " sec to write and "
<< diff(final, mid2) << " sec to read" << endl;
return;
}


void timeLongUnrolledTest() {

struct timeval init, mid1, mid2, final;
gettimeofday(&init, 0);

//
long long* longs = new long long[length/8];

gettimeofday(&mid1, 0);
for (long long i = 0; i < length/8; i += 4) {
longs[i] = i;
longs[i + 1] = i + 1;
longs[i + 2] = i + 2;
longs[i + 3] = i + 3;
}

gettimeofday(&mid2, 0);

for (long long i = 0; i < length/8; i += 4) {
long long b0 = longs[i];
long long b1 = longs[i + 1];
long long b2 = longs[i + 2];
long long b3 = longs[i + 3];
if (b0 != i) {
cerr << "Expected " << i << " but was " << b0 << endl;
return;
}
if (b1 != (i + 1)) {
cerr << "Expected " << (i + 1) << " but was " << b1 << endl;
return;
}
if (b2 != (i + 2)) {
cerr << "Expected " << (i + 2) << " but was " << b2 << endl;
return;
}
if (b3 != (i + 3)) {
cerr << "Expected " << (i + 3) << " but was " << b3 << endl;
return;
}
}
gettimeofday(&final, 0);
delete longs;

//
cout << "long[] unrolled took "
<< diff(mid1, init)*1e6 << " usec to allocate and "
<< diff(mid2, mid1) << " sec to write and "
<< diff(final, mid2) << " sec to read" << endl;
return;
}

/*
*
*/
int main(int argc, char** argv) {
// timeBytesTest();
// timeBytesUnrolledTest();
// timeLongTest();
timeLongUnrolledTest();
return 0;
}

Change log

r56 by peter.lawrey on Aug 29, 2011   Diff
Shoot out benchmarks.
Go to: 
Project members, sign in to write a code review

Older revisions

r47 by peter.lawrey on Aug 4, 2011   Diff
Memory exerciser in C++
All revisions of this file

File info

Size: 4879 bytes, 203 lines
Powered by Google Project Hosting