Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
JMemoryUsage.cc
Go to the documentation of this file.
1#include <string>
2#include <iostream>
3#include <iomanip>
4#include <vector>
5#include <unistd.h>
6
7#include "JSystem/JShell.hh"
9
10#include "Jeep/JPrint.hh"
11#include "Jeep/JParser.hh"
12#include "Jeep/JMessage.hh"
13
14
15/**
16 * \file
17 *
18 * Example program to show memory usage.
19 * \author mdejong
20 */
21int main(int argc, char **argv)
22{
23 using namespace std;
24 using namespace JPP;
25
26 size_t size_B;
27 unsigned int sleep_us;
28 bool use;
29 int debug;
30
31 try {
32
33 JParser<> zap("Example program to show memory usage.");
34
35 zap['M'] = make_field(size_B) = 1<<25;
36 zap['s'] = make_field(sleep_us) = 1;
37 zap['u'] = make_field(use);
38 zap['d'] = make_field(debug) = 3;
39
40 zap(argc, argv);
41 }
42 catch(const exception &error) {
43 FATAL(error.what() << endl);
44 }
45
46 if (sleep_us == 0) {
47 sleep_us = 1;
48 }
49
50 typedef long long int data_type;
51
52 const size_t size = size_B / sizeof(data_type);
53
54 {
55 cout << endl;
56 cout << "Memory usage (start) " << FIXED(5,2) << getMemoryUsage() << "%" << endl;
57
58 data_type* buffer = new data_type[size];
59
60 usleep(sleep_us);
61
62 cout << "Memory usage (new) " << FIXED(5,2) << getMemoryUsage() << "%" << endl;
63
64 if (use) {
65
66 for (size_t i = 0; i != size; ++i) {
67 buffer[i] = i;
68 }
69
70 usleep(sleep_us);
71
72 cout << "Memory usage (assign) " << FIXED(5,2) << getMemoryUsage() << "%" << endl;
73 }
74
75 delete[] buffer;
76
77 usleep(sleep_us);
78
79 cout << "Memory usage (delete) " << FIXED(5,2) << getMemoryUsage() << "%" << endl;
80 }
81 {
82 cout << endl;
83 cout << "Memory usage (start) " << FIXED(5,2) << getMemoryUsage() << "%" << endl;
84
85 vector<data_type> buffer;
86
87 buffer.reserve(size);
88
89 cout << "Memory usage (vector) " << FIXED(5,2) << getMemoryUsage() << "%" << endl;
90
91 if (use) {
92
93 for (size_t i = 0; i != size; ++i) {
94 buffer.push_back(i);
95 }
96
97 usleep(sleep_us);
98
99 cout << "Memory usage (push_back) " << FIXED(5,2) << getMemoryUsage() << "%" << endl;
100 }
101
102 buffer.clear();
103
104 usleep(sleep_us);
105
106 cout << "Memory usage (clear) " << FIXED(5,2) << getMemoryUsage() << "%" << endl;
107
108 {
110
111 buffer.swap(zbuf);
112 }
113
114 usleep(sleep_us);
115
116 cout << "Memory usage (swap) " << FIXED(5,2) << getMemoryUsage() << "%" << endl;
117 }
118 {
119 cout << endl;
120 cout << "Memory usage (start) " << FIXED(5,2) << getMemoryUsage() << "%" << endl;
121
122 size_t N = 10;
123
124 vector< vector<data_type> > buffer(N);
125
126 if (use) {
127
128 for (size_t i = 0; i != N; ++i) {
129 buffer[i].resize(size / N);
130 }
131
132 usleep(sleep_us);
133
134 cout << "Memory usage (vector) " << FIXED(5,2) << getMemoryUsage() << "%" << endl;
135 }
136
137 buffer.clear();
138
139 usleep(sleep_us);
140
141 cout << "Memory usage (clear) " << FIXED(5,2) << getMemoryUsage() << "%" << endl;
142
143 {
145
146 buffer.swap(zbuf);
147 }
148
149 usleep(sleep_us);
150
151 cout << "Memory usage (swap) " << FIXED(5,2) << getMemoryUsage() << "%" << endl;
152 }
153}
int main(int argc, char **argv)
General purpose messaging.
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
I/O formatting auxiliaries.
Shell interaction via I/O streams.
System auxiliaries.
Utility class to parse command line options.
Definition JParser.hh:1698
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary data structure for floating point format specification.
Definition JManip.hh:448
Data structure for measured coincidence rates of all pairs of PMTs in optical module.
Definition JFitK40.hh:103