-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcbreakdown.cc
78 lines (73 loc) · 2.19 KB
/
cbreakdown.cc
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
#include <string>
#include <boost/shared_array.hpp>
#include <boost/shared_ptr.hpp>
#include <sys/resource.h>
#include <sys/time.h>
#include <iostream>
#include <iomanip>
using namespace boost;
using namespace std;
#define W 25
#define N 10
class timer
{
public:
timer(void) { reset(); }
void reset(void) {
gettimeofday( &m_time, NULL );
getrusage( RUSAGE_SELF, &m_usage );
}
void report(void) const;
const rusage &usage(void) const { return m_usage; }
const timeval &time(void) const { return m_time; }
protected:
struct timeval m_time;
struct rusage m_usage;
};
ostream &operator<<( ostream &stream, const timer &t )
{
struct timeval current;
gettimeofday( ¤t, NULL );
struct rusage actual;
getrusage( RUSAGE_SELF, &actual );
struct timeval user;
struct timeval system;
struct timeval real;
timersub( &actual.ru_utime, &t.usage().ru_utime, &user );
timersub( &actual.ru_stime, &t.usage().ru_stime, &system );
timersub( ¤t, &t.time(), &real );
stream << setw( N ) << ( user.tv_sec + user.tv_usec * 1.0e-6 )
<< ' ' << setw( N - 1 ) << ( system.tv_sec + system.tv_usec * 1.0e-6 )
<< ' ' << setw( N - 1 ) << ( user.tv_sec + user.tv_usec * 1.0e-6 +
system.tv_sec + system.tv_usec * 1.0e-6 )
<< " (" << setw( N - 1 )
<< ( real.tv_sec + real.tv_usec * 1.0e-6 ) << ')';
return stream;
}
int main(void) {
int w = 1000000;
int c = 1000;
boost::shared_array<unsigned char> n( new unsigned char[w] );
cout << setw( W + N ) << "user" << setw( N ) << "system"
<< setw( N ) << "total" << setw( N + 2 ) << "real" << endl;
{
timer t;
for ( int i=0; i<c; i++ ) {
boost::shared_array< unsigned char > result( new unsigned char[w] );
unsigned char *p = n.get();
unsigned char *qend = result.get() + w;
for ( unsigned char *q = result.get(); q != qend; p++, q++ )
*q = -*p;
};
cout << setw( W ) << "optimised C" << t << endl;
};
{
timer t;
for ( int i=0; i<c; i++ ) {
boost::shared_array< unsigned char > result( new unsigned char[w] );
result.get();
};
cout << setw( W ) << "allocation only" << t << endl;
};
return 0;
};