libsaria: Print format_cache stats
I wanted to know how often there were cache hits and misses, so I print out these stats on shutdown.
This commit is contained in:
parent
664ad0dad2
commit
52a3e73023
|
@ -8,7 +8,8 @@ using namespace std;
|
|||
namespace libsaria
|
||||
{
|
||||
|
||||
list<string> *format_text(string &);
|
||||
list<string> *format_text(const string &);
|
||||
void print_format_stats();
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1,13 +1,16 @@
|
|||
|
||||
#include <libsaria/format.h>
|
||||
#include <libsaria/print.h>
|
||||
|
||||
#include <map>
|
||||
#include <list>
|
||||
using namespace std;
|
||||
|
||||
static map<string, list<string> > format_cache;
|
||||
static unsigned int hits;
|
||||
static unsigned int misses;
|
||||
|
||||
list<string> *do_format(string &text)
|
||||
list<string> *do_format(const string &text)
|
||||
{
|
||||
string word;
|
||||
list<string> word_list;
|
||||
|
@ -60,16 +63,24 @@ list<string> *do_format(string &text)
|
|||
namespace libsaria
|
||||
{
|
||||
|
||||
list<string> *format_text(string &text)
|
||||
list<string> *format_text(const string &text)
|
||||
{
|
||||
map<string, list<string> >::iterator it;
|
||||
it = format_cache.find(text);
|
||||
|
||||
/* Not found in cache... */
|
||||
if (it == format_cache.end())
|
||||
if (it == format_cache.end()) {
|
||||
misses++;
|
||||
return do_format(text);
|
||||
else
|
||||
} else {
|
||||
hits++;
|
||||
return &(it->second);
|
||||
}
|
||||
}
|
||||
|
||||
void print_format_stats()
|
||||
{
|
||||
println("Format cache hits: %u misses: %u", hits, misses);
|
||||
}
|
||||
|
||||
} /* Namespace: libsaria */
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include <libsaria/prefs.h>
|
||||
#include <libsaria/print.h>
|
||||
#include <libsaria/library.h>
|
||||
#include <libsaria/format.h>
|
||||
|
||||
namespace libsaria
|
||||
{
|
||||
|
@ -25,6 +26,7 @@ namespace libsaria
|
|||
void quit()
|
||||
{
|
||||
println("Quitting libsaria");
|
||||
print_format_stats();
|
||||
}
|
||||
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue