source: proto/SymbolTable/symbol_stat_gatherer.h @ 1786

Last change on this file since 1786 was 1786, checked in by vla24, 8 years ago

Added some code to support performance tests using PAPI

File size: 3.4 KB
Line 
1#include <vector>
2#include <limits.h>
3#include <algorithm>
4
5using namespace std;
6
7bool comparison (int i,int j) { return (i<j); }
8
9class SymbolStatGatherer
10{
11public:
12    void print_symbol_statistic();
13    void store_symbol_density(int symbol_start, int symbol_end);
14    void store_symbol_length(int L);
15    void store_symbol_gid(int GID);
16
17private:
18    vector <int> symbol_lgth;
19    vector <int> density_starts;
20    vector <int> density_ends;
21    vector <int> gids;
22
23    vector<int> compute_symbol_density(vector<int> density_starts, vector<int> density_ends);
24    int min(vector<int>v);
25    int max(vector<int>v);
26    float total(vector<int>v);
27    double average(vector<int>v);
28};
29
30void SymbolStatGatherer::store_symbol_density(int symbol_start, int symbol_end)
31{
32    if (!density_starts.size())
33    {
34        density_starts.push_back(0);
35    }
36
37    density_ends.push_back(symbol_start);
38
39    if (symbol_end > -1)
40    {
41        density_starts.push_back(symbol_end);
42    }
43}
44
45void SymbolStatGatherer::store_symbol_length(int L)
46{
47    symbol_lgth.push_back(L);
48}
49
50void SymbolStatGatherer::store_symbol_gid(int GID)
51{
52    gids.push_back(GID);
53}
54
55void SymbolStatGatherer::print_symbol_statistic()
56{
57//    symbol counts
58    printf ("Total symbols: %i\n", gids.size());
59
60//    symbol length
61    printf ("Min length: %i \n", min(symbol_lgth));
62    printf ("Max length: %i \n", max(symbol_lgth));
63    printf ("Avg length: %f \n", average(symbol_lgth));
64
65//    frequency of unique symbols
66    printf ("Total unique symbols: %i\n", max(gids));
67
68//    symbol density (symbol separation distance)
69    vector<int> symbol_density = compute_symbol_density(density_starts, density_ends);
70//    printf ("Avg symbol density: %f\n", average(symbol_density));
71    float total_lgth = total(symbol_lgth);
72    float total_delimiters = total(symbol_density);
73    printf ("Symbol density: %f\n", total_lgth/(total_lgth+total_delimiters));
74}
75
76vector<int> SymbolStatGatherer::compute_symbol_density(vector<int> density_starts, vector<int> density_ends)
77{
78    vector<int> symbol_density;
79    int size = density_starts.size();
80    sort (density_starts.begin(), density_starts.end(), comparison);
81    sort (density_ends.begin(), density_ends.end(), comparison);
82
83    for(int i = 0; i < size; i++)
84    {
85        int start, end, density;
86        start = density_starts[i];
87        end = density_ends[i];
88        density = end - start;
89
90        symbol_density.push_back(density);
91    }
92    return symbol_density;
93}
94
95int SymbolStatGatherer::min(vector<int> v)
96{
97    int result = INT_MAX;
98    for (vector<int>::iterator it = v.begin(); it < v.end(); it++)
99    {
100        int number = *it;
101
102        if (number < result)
103        {
104            result = number;
105        }
106    }
107    return result;
108}
109
110float SymbolStatGatherer::total(vector<int> v)
111{
112    float result = 0;
113    for (vector<int>::iterator it = v.begin(); it < v.end(); it++)
114    {
115        int number = *it;
116        result += number;
117    }
118    return result;
119}
120
121int SymbolStatGatherer::max(vector<int> v)
122{
123    int result = INT_MIN;
124    for (vector<int>::iterator it = v.begin(); it < v.end(); it++)
125    {
126        int number = *it;
127
128        if (number > result)
129        {
130            result = number;
131        }
132    }
133    return result;
134}
135
136double SymbolStatGatherer::average(vector<int> v)
137{
138    double result = 0.0;
139    for (vector<int>::iterator it = v.begin(); it < v.end(); it++)
140    {
141        int number = *it;
142
143        result += number;
144    }
145    return result/v.size();
146}
Note: See TracBrowser for help on using the repository browser.