I am writing a containers library in C. The generic list container
adds for a new type:
code: 2495 bytes
data: 488 bytes
for a list container containing around 60 entry points.
This means that you will pay 2983 bytes for each instatiation
of the list template (one of the biggest around)
I would be interested to know the corresponding vaue of the
C++ stl. How many bytes of code it will cost you to instantiate
a list template for a new type?
Rather than fussing over a few bytes, how about a performance comparison?
Here's a simple (related to some work I have been doing recently) benchmark:
Generate a list of 10,000,000 random longs (call it random)
use that list to instantiate another list (call it list)
sort list
sum and empty list by removing the first entry until empty.
What would be your containers library solution and how does it compare
to this C++ solution?
#include <list>
#include <iostream>
#include <stdint.h>
#include <stdlib.h>
// For system hi-res timer, add your own here.
//
int64_t hiresTime();
const unsigned entries = 10000000;
typedef std::list<long> List;
void fill( List& list )
{
srand48(42);
for( unsigned n = 0; n < entries; ++n )
list.push_back(lrand48());
}
uint64_t get( List& list )
{
uint64_t result(0);
while( !list.empty() )
{
result += list.front();
list.pop_front();
}
return result;
}
double delta( int64_t start, int64_t now )
{
return (now-start) / 1000000000.0;
}
int main()
{
using std::cout;
using std::endl;
List random;
int64_t start = hiresTime();
fill( random );
int64_t now = hiresTime();
cout << "To generate " << delta(start, now) << 'S' << endl;
start = now;
List list(random);
now = hiresTime();
cout << "To create " << delta(start, now) << 'S' << endl;
start = now;
list.sort();
now = hiresTime();
cout << "To sort " << delta(start, now) << 'S' << endl;
start = now;
uint64_t n = get( list );
now = hiresTime();
cout << "To empty " << delta(start, now) << 'S' << endl;
cout << n << endl;
}