A work colleague (AB) discovered a little trick with pg profiling that everyone should know about if they don't already,
Profiling is often avoided in live apps because it is expensive and slows down apps that are already running slow (why else profile?)
But there are two things going on when profiling. If you COMPILE with a -pg, every function that gets called stores information about itself. This info is used to determine how many times particular functions are called, and which function gets called by which function. Unfortunately this is expensive for your application, especially for properly written c++ apps as functions are called lots and lots.
When you LINK with a -pg, the program will sample your application 100 times a second and record which function is active at that point in time. This information is used to estimate what proportion of time your app spends in each function. This is often the most sought after information when profiling and is actually very cheap, computationally.
To come to these conclusions I wrote a test application that had a handful of functions that get called a lot, one of them was a recursive function. My results with different profiling methods were as follows:
(1) No profiling: application took 13 seconds to execute.
(2) Linked with -pg: application took 13 seconds to execute.
(3) Compiled and Linked with -pg: application took 59 seconds to execute.
When linking with -pg the call graph was absent from the profiling results, but the percentage of time spent in each function was retained with accurate results.
All of the interesting technological, artistic or just plain fun subjects I'd investigate if I had an infinite number of lifetimes. In other words, a dumping ground...
Monday, 19 July 2010
Monday, 12 July 2010
Using zlib in C programming
g++ -Wall -Wextra comp.cpp -lz -o comp
#include <iostream>
#include <cstring>
#include <zlib.h>
using namespace std;
/*
ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen,
const Bytef *source, uLong sourceLen));
Compresses the source buffer into the destination buffer. sourceLen is
the byte length of the source buffer. Upon entry, destLen is the total
size of the destination buffer, which must be at least the value returned
by compressBound(sourceLen). Upon exit, destLen is the actual size of the
compressed buffer.
This function can be used to compress a whole file at once if the
input file is mmap'ed.
compress returns Z_OK if success, Z_MEM_ERROR if there was not
enough memory, Z_BUF_ERROR if there was not enough room in the output
buffer.
ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
const Bytef *source, uLong sourceLen));
Decompresses the source buffer into the destination buffer. sourceLen is
the byte length of the source buffer. Upon entry, destLen is the total
size of the destination buffer, which must be large enough to hold the
entire uncompressed data. (The size of the uncompressed data must have
been saved previously by the compressor and transmitted to the decompressor
by some mechanism outside the scope of this compression library.)
Upon exit, destLen is the actual size of the compressed buffer.
This function can be used to decompress a whole file at once if the
input file is mmap'ed.
uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
enough memory, Z_BUF_ERROR if there was not enough room in the output
buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete.
*/
int main()
{
char d[] =
"QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
cout << "sizeof(d): " << sizeof(d) << endl;
const unsigned int DESTLEN = 256;
char dest[DESTLEN];
if (sizeof(d) > DESTLEN) {
cerr << "Dest size " << DESTLEN << " too small : " << sizeof(d) << endl;
return 0;
}
uLongf destlen = DESTLEN;
unsigned int compress_bound = compressBound(sizeof(d));
cout << "Source len: " << sizeof(d) << ", size of compress bound: " << compress_bound << ", size of destlen: " <<
DESTLEN << endl;
if (compress_bound > DESTLEN) {
cerr << "Dest size " << DESTLEN << " too small for compress_bound: " << compress_bound << endl;
return 0;
}
int rtn = compress((Bytef*)dest, &destlen, (Bytef*)d, sizeof(d));
if (rtn != Z_OK) {
cerr << "Compress failed" << rtn << endl;
} else {
cout << "Compress suceeded: destlen: " << destlen << endl;
//cout << "Compressed: " << dest << endl;
}
char dest2[DESTLEN] = {0};
uLongf destlen2 = DESTLEN;
rtn = uncompress((Bytef*)dest2, &destlen2, (Bytef*)dest, destlen);
if (rtn != Z_OK) {
cerr << "Decompress failed" << rtn << endl;
} else {
cout << "Decompress suceeded: destlen: " << destlen2 << endl;
cout << "Decompressed: " << dest2 << endl;
}
rtn = memcmp(d, dest2, sizeof(d));
cout << "Comparison: " << rtn << endl;
return 0;
}
Wednesday, 7 July 2010
Monday, 5 July 2010
Subscribe to:
Posts (Atom)