|
12 | 12 | #include "string_container.h"
|
13 | 13 |
|
14 | 14 | #include <cstring>
|
| 15 | +#include <iostream> |
| 16 | +#include <numeric> |
15 | 17 |
|
16 | 18 | string_ptrt::string_ptrt(const char *_s):s(_s), len(strlen(_s))
|
17 | 19 | {
|
@@ -74,3 +76,36 @@ unsigned string_containert::get(const std::string &s)
|
74 | 76 |
|
75 | 77 | return r;
|
76 | 78 | }
|
| 79 | + |
| 80 | +void string_container_statisticst::dump_on_stream(std::ostream &out) const |
| 81 | +{ |
| 82 | + auto total_memory_usage = strings_memory_usage + vector_memory_usage + |
| 83 | + map_memory_usage + list_memory_usage; |
| 84 | + out << "String container statistics:" |
| 85 | + << "\n string count: " << string_count |
| 86 | + << "\n string memory usage: " << strings_memory_usage.to_string() |
| 87 | + << "\n vector memory usage: " << vector_memory_usage.to_string() |
| 88 | + << "\n map memory usage: " << map_memory_usage.to_string() |
| 89 | + << "\n list memory usage: " << list_memory_usage.to_string() |
| 90 | + << "\n total memory usage: " << total_memory_usage.to_string() << '\n'; |
| 91 | +} |
| 92 | + |
| 93 | +string_container_statisticst string_containert::compute_statistics() const |
| 94 | +{ |
| 95 | + string_container_statisticst result; |
| 96 | + result.string_count = string_vector.size(); |
| 97 | + result.vector_memory_usage = memory_sizet::from_bytes( |
| 98 | + sizeof(string_vector) + |
| 99 | + sizeof(string_vectort::value_type) * string_vector.capacity()); |
| 100 | + result.strings_memory_usage = memory_sizet::from_bytes(std::accumulate( |
| 101 | + begin(string_vector), |
| 102 | + end(string_vector), |
| 103 | + std::size_t(0), |
| 104 | + [](std::size_t sz, const std::string *s) { return sz + s->capacity(); })); |
| 105 | + result.map_memory_usage = memory_sizet::from_bytes( |
| 106 | + sizeof(hash_table) + hash_table.size() * sizeof(hash_tablet::value_type)); |
| 107 | + |
| 108 | + result.list_memory_usage = memory_sizet::from_bytes( |
| 109 | + sizeof(string_list) + 2 * sizeof(void *) * string_list.size()); |
| 110 | + return result; |
| 111 | +} |
0 commit comments