timer.cc
Go to the documentation of this file.
1 // base/timer.cc
2 
3 // Copyright 2018 Johns Hopkins University (author: Daniel Povey)
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 
11 // http://www.apache.org/licenses/LICENSE-2.0
12 
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 #include "base/timer.h"
21 #include "base/kaldi-error.h"
22 #include <algorithm>
23 #include <iomanip>
24 #include <map>
25 #include <unordered_map>
26 
27 namespace kaldi {
28 
29 class ProfileStats {
30  public:
31  void AccStats(const char *function_name, double elapsed) {
32  std::unordered_map<const char*, ProfileStatsEntry>::iterator
33  iter = map_.find(function_name);
34  if (iter == map_.end()) {
35  map_[function_name] = ProfileStatsEntry(function_name);
36  map_[function_name].total_time = elapsed;
37  } else {
38  iter->second.total_time += elapsed;
39  }
40  }
42  // This map makes sure we agglomerate the time if there were any duplicate
43  // addresses of strings.
44  std::unordered_map<std::string, double> total_time;
45  for (auto iter = map_.begin(); iter != map_.end(); iter++)
46  total_time[iter->second.name] += iter->second.total_time;
47 
49  std::vector<std::pair<std::string, double> > pairs(total_time.begin(),
50  total_time.end());
51  std::sort(pairs.begin(), pairs.end(), comp);
52  for (size_t i = 0; i < pairs.size(); i++) {
53  KALDI_LOG << "Time taken in " << pairs[i].first << " is "
54  << std::fixed << std::setprecision(2) << pairs[i].second << "s.";
55  }
56  }
57  private:
58 
60  std::string name;
61  double total_time;
63  ProfileStatsEntry(const char *name): name(name) { }
64  };
65 
67  bool operator () (const std::pair<std::string, double> &a,
68  const std::pair<std::string, double> &b) {
69  return a.second > b.second;
70  }
71  };
72 
73  // Note: this map is keyed on the address of the string, there is no proper
74  // hash function. The assumption is that the strings are compile-time
75  // constants.
76  std::unordered_map<const char*, ProfileStatsEntry> map_;
77 };
78 
80 
82  g_profile_stats.AccStats(name_, tim_.Elapsed());
83 }
84 
85 } // namespace kaldi
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
std::unordered_map< const char *, ProfileStatsEntry > map_
Definition: timer.cc:76
ProfileStatsEntry(const char *name)
Definition: timer.cc:63
void AccStats(const char *function_name, double elapsed)
Definition: timer.cc:31
#define KALDI_LOG
Definition: kaldi-error.h:153
ProfileStats g_profile_stats
Definition: timer.cc:79