यदि आपको ऐसा कोई तुलना चार्ट नहीं मिलता है, तो विकल्प यह है कि आप एसटीएल कक्षाओं के प्रश्न में स्वयं के आवंटनकर्ता को इंजेक्ट करें और कुछ लॉगिंग जोड़ें।
मैंने जिस कार्यान्वयन (वीसी 8.0) का परीक्षण किया है, वह केवल स्ट्रिंग / वेक्टर / डीके की घोषणा करके कोई स्मृति आवंटन का उपयोग नहीं करता है, लेकिन इसके लिए यह सूची और मानचित्र करता है। स्ट्रिंग में एक शॉर्ट स्ट्रिंग ऑप्टिमाइज़ेशन है, क्योंकि 3 चार्ट जोड़ने से आवंटन नहीं हुआ। आउटपुट कोड के नीचे जोड़ा जाता है।
// basic allocator implementation used from here
// http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4079
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <map>
template <class T> class my_allocator;
// specialize for void:
template <>
class my_allocator<void>
{
public:
typedef void* pointer;
typedef const void* const_pointer;
// reference to void members are impossible.
typedef void value_type;
template <class U>
struct rebind
{
typedef my_allocator<U> other;
};
};
#define LOG_ALLOC_SIZE(call, size) std::cout << " " << call << " " << std::setw(2) << size << " byte" << std::endl
template <class T>
class my_allocator
{
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
template <class U>
struct rebind
{
typedef my_allocator<U> other;
};
my_allocator() throw() : alloc() {}
my_allocator(const my_allocator&b) throw() : alloc(b.alloc) {}
template <class U> my_allocator(const my_allocator<U>&b) throw() : alloc(b.alloc) {}
~my_allocator() throw() {}
pointer address(reference x) const { return alloc.address(x); }
const_pointer address(const_reference x) const { return alloc.address(x); }
pointer allocate(size_type s,
my_allocator<void>::const_pointer hint = 0) { LOG_ALLOC_SIZE("my_allocator::allocate ", s * sizeof(T)); return alloc.allocate(s, hint); }
void deallocate(pointer p, size_type n) { LOG_ALLOC_SIZE("my_allocator::deallocate", n * sizeof(T)); alloc.deallocate(p, n); }
size_type max_size() const throw() { return alloc.max_size(); }
void construct(pointer p, const T& val) { alloc.construct(p, val); }
void destroy(pointer p) { alloc.destroy(p); }
std::allocator<T> alloc;
};
int main(int argc, char *argv[])
{
{
typedef std::basic_string<char, std::char_traits<char>, my_allocator<char> > my_string;
std::cout << "===============================================" << std::endl;
std::cout << "my_string ctor start" << std::endl;
my_string test;
std::cout << "my_string ctor end" << std::endl;
std::cout << "my_string add 3 chars" << std::endl;
test = "abc";
std::cout << "my_string add a huge number of chars chars" << std::endl;
test += "d df uodfug ondusgp idugnösndögs ifdögsdoiug ösodifugnösdiuödofu odsugöodiu niu od unoudö n nodsu nosfdi un abc";
std::cout << "my_string copy" << std::endl;
my_string copy = test;
std::cout << "my_string copy on write test" << std::endl;
copy[3] = 'X';
std::cout << "my_string dtors start" << std::endl;
}
{
std::cout << std::endl << "===============================================" << std::endl;
std::cout << "vector ctor start" << std::endl;
std::vector<int, my_allocator<int> > v;
std::cout << "vector ctor end" << std::endl;
for(int i = 0; i < 5; ++i)
{
v.push_back(i);
}
std::cout << "vector dtor starts" << std::endl;
}
{
std::cout << std::endl << "===============================================" << std::endl;
std::cout << "deque ctor start" << std::endl;
std::deque<int, my_allocator<int> > d;
std::cout << "deque ctor end" << std::endl;
for(int i = 0; i < 5; ++i)
{
std::cout << "deque insert start" << std::endl;
d.push_back(i);
std::cout << "deque insert end" << std::endl;
}
std::cout << "deque dtor starts" << std::endl;
}
{
std::cout << std::endl << "===============================================" << std::endl;
std::cout << "list ctor start" << std::endl;
std::list<int, my_allocator<int> > l;
std::cout << "list ctor end" << std::endl;
for(int i = 0; i < 5; ++i)
{
std::cout << "list insert start" << std::endl;
l.push_back(i);
std::cout << "list insert end" << std::endl;
}
std::cout << "list dtor starts" << std::endl;
}
{
std::cout << std::endl << "===============================================" << std::endl;
std::cout << "map ctor start" << std::endl;
std::map<int, float, std::less<int>, my_allocator<std::pair<const int, float> > > m;
std::cout << "map ctor end" << std::endl;
for(int i = 0; i < 5; ++i)
{
std::cout << "map insert start" << std::endl;
std::pair<int, float> a(i, (float)i);
m.insert(a);
std::cout << "map insert end" << std::endl;
}
std::cout << "map dtor starts" << std::endl;
}
return 0;
}
अब तक VC8 और STLPort 5.2 का परीक्षण किया गया है, यहाँ तुलना है (परीक्षण में शामिल: स्ट्रिंग, वेक्टर, डीके, सूची, मानचित्र)
Allocation on declare Overhead List Node Overhead Map Node
VC8 map, list 8 Byte 16 Byte
STLPort 5.2 (VC8) deque 8 Byte 16 Byte
Paulhodge's EASTL (none) 8 Byte 16 Byte
VC8 आउटपुट स्ट्रिंग / वेक्टर / deque / सूची / नक्शा:
===============================================
my_string ctor start
my_string ctor end
my_string add 3 chars
my_string add a huge number of chars chars
my_allocator::allocate 128 byte
my_string copy
my_allocator::allocate 128 byte
my_string copy on write test
my_string dtors start
my_allocator::deallocate 128 byte
my_allocator::deallocate 128 byte
===============================================
vector ctor start
vector ctor end
my_allocator::allocate 4 byte
my_allocator::allocate 8 byte
my_allocator::deallocate 4 byte
my_allocator::allocate 12 byte
my_allocator::deallocate 8 byte
my_allocator::allocate 16 byte
my_allocator::deallocate 12 byte
my_allocator::allocate 24 byte
my_allocator::deallocate 16 byte
vector dtor starts
my_allocator::deallocate 24 byte
===============================================
deque ctor start
deque ctor end
deque insert start
my_allocator::allocate 32 byte
my_allocator::allocate 16 byte
deque insert end
deque insert start
deque insert end
deque insert start
deque insert end
deque insert start
deque insert end
deque insert start
my_allocator::allocate 16 byte
deque insert end
deque dtor starts
my_allocator::deallocate 16 byte
my_allocator::deallocate 16 byte
my_allocator::deallocate 32 byte
===============================================
list ctor start
my_allocator::allocate 12 byte
list ctor end
list insert start
my_allocator::allocate 12 byte
list insert end
list insert start
my_allocator::allocate 12 byte
list insert end
list insert start
my_allocator::allocate 12 byte
list insert end
list insert start
my_allocator::allocate 12 byte
list insert end
list insert start
my_allocator::allocate 12 byte
list insert end
list dtor starts
my_allocator::deallocate 12 byte
my_allocator::deallocate 12 byte
my_allocator::deallocate 12 byte
my_allocator::deallocate 12 byte
my_allocator::deallocate 12 byte
my_allocator::deallocate 12 byte
===============================================
map ctor start
my_allocator::allocate 24 byte
map ctor end
map insert start
my_allocator::allocate 24 byte
map insert end
map insert start
my_allocator::allocate 24 byte
map insert end
map insert start
my_allocator::allocate 24 byte
map insert end
map insert start
my_allocator::allocate 24 byte
map insert end
map insert start
my_allocator::allocate 24 byte
map insert end
map dtor starts
my_allocator::deallocate 24 byte
my_allocator::deallocate 24 byte
my_allocator::deallocate 24 byte
my_allocator::deallocate 24 byte
my_allocator::deallocate 24 byte
my_allocator::deallocate 24 byte
STLPort 5.2। आउटपुट VC8 के साथ संकलित
===============================================
my_string ctor start
my_string ctor end
my_string add 3 chars
my_string add a huge number of chars chars
my_allocator::allocate 115 byte
my_string copy
my_allocator::allocate 115 byte
my_string copy on write test
my_string dtors start
my_allocator::deallocate 115 byte
my_allocator::deallocate 115 byte
===============================================
vector ctor start
vector ctor end
my_allocator::allocate 4 byte
my_allocator::deallocate 0 byte
my_allocator::allocate 8 byte
my_allocator::deallocate 4 byte
my_allocator::allocate 16 byte
my_allocator::deallocate 8 byte
my_allocator::allocate 32 byte
my_allocator::deallocate 16 byte
vector dtor starts
my_allocator::deallocate 32 byte
===============================================
deque ctor start
my_allocator::allocate 32 byte
my_allocator::allocate 128 byte
deque ctor end
deque insert start
deque insert end
deque insert start
deque insert end
deque insert start
deque insert end
deque insert start
deque insert end
deque insert start
deque insert end
deque dtor starts
my_allocator::deallocate 128 byte
my_allocator::deallocate 32 byte
===============================================
list ctor start
list ctor end
list insert start
my_allocator::allocate 12 byte
list insert end
list insert start
my_allocator::allocate 12 byte
list insert end
list insert start
my_allocator::allocate 12 byte
list insert end
list insert start
my_allocator::allocate 12 byte
list insert end
list insert start
my_allocator::allocate 12 byte
list insert end
list dtor starts
my_allocator::deallocate 12 byte
my_allocator::deallocate 12 byte
my_allocator::deallocate 12 byte
my_allocator::deallocate 12 byte
my_allocator::deallocate 12 byte
===============================================
map ctor start
map ctor end
map insert start
my_allocator::allocate 24 byte
map insert end
map insert start
my_allocator::allocate 24 byte
map insert end
map insert start
my_allocator::allocate 24 byte
map insert end
map insert start
my_allocator::allocate 24 byte
map insert end
map insert start
my_allocator::allocate 24 byte
map insert end
map dtor starts
my_allocator::deallocate 24 byte
my_allocator::deallocate 24 byte
my_allocator::deallocate 24 byte
my_allocator::deallocate 24 byte
my_allocator::deallocate 24 byte
ईएएसटीएल के परिणाम, कोई डीके उपलब्ध नहीं है
===============================================
my_string ctor start
my_string ctor end
my_string add 3 chars
my_allocator::allocate 9 byte
my_string add a huge number of chars chars
my_allocator::allocate 115 byte
my_allocator::deallocate 9 byte
my_string copy
my_allocator::allocate 115 byte
my_string copy on write test
my_string dtors start
my_allocator::deallocate 115 byte
my_allocator::deallocate 115 byte
===============================================
vector ctor start
vector ctor end
my_allocator::allocate 4 byte
my_allocator::allocate 8 byte
my_allocator::deallocate 4 byte
my_allocator::allocate 16 byte
my_allocator::deallocate 8 byte
my_allocator::allocate 32 byte
my_allocator::deallocate 16 byte
vector dtor starts
my_allocator::deallocate 32 byte
===============================================
list ctor start
list ctor end
list insert start
my_allocator::allocate 12 byte
list insert end
list insert start
my_allocator::allocate 12 byte
list insert end
list insert start
my_allocator::allocate 12 byte
list insert end
list insert start
my_allocator::allocate 12 byte
list insert end
list insert start
my_allocator::allocate 12 byte
list insert end
list dtor starts
my_allocator::deallocate 12 byte
my_allocator::deallocate 12 byte
my_allocator::deallocate 12 byte
my_allocator::deallocate 12 byte
my_allocator::deallocate 12 byte
===============================================
map ctor start
map ctor end
map insert start
my_allocator::allocate 24 byte
map insert end
map insert start
my_allocator::allocate 24 byte
map insert end
map insert start
my_allocator::allocate 24 byte
map insert end
map insert start
my_allocator::allocate 24 byte
map insert end
map insert start
my_allocator::allocate 24 byte
map insert end
map dtor starts
my_allocator::deallocate 24 byte
my_allocator::deallocate 24 byte
my_allocator::deallocate 24 byte
my_allocator::deallocate 24 byte
my_allocator::deallocate 24 byte
deque
हमेशा वेक्टर के साथ एसटीएल में लागू किया गया था।