Skip to content

Commit e61c01d

Browse files
authored
Use unordered_set for nonsilence lookup in GetSpkVector (#2035)
Replaces O(n^2) std::find scan with O(1)-avg hash-set lookup, addressing the existing "would be nice to have faster search" comment. Total cost drops from O(num_frames * |nonsilence|) to O(num_frames + |nonsilence|).
1 parent 3bc886a commit e61c01d

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

src/recognizer.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "lat/sausages.h"
1919
#include "language_model.h"
2020

21+
#include <unordered_set>
22+
2123
using namespace fst;
2224
using namespace kaldi::nnet3;
2325

@@ -462,13 +464,12 @@ bool Recognizer::GetSpkVector(Vector<BaseFloat> &out_xvector, int *num_spk_frame
462464
int num_frames = spk_feature_->NumFramesReady() - frame_offset_ * 3;
463465
Matrix<BaseFloat> mfcc(num_frames, spk_feature_->Dim());
464466

465-
// Not very efficient, would be nice to have faster search
467+
std::unordered_set<int32> nonsilence_set(nonsilence_frames.begin(), nonsilence_frames.end());
466468
int num_nonsilence_frames = 0;
467469
Vector<BaseFloat> feat(spk_feature_->Dim());
468470

469471
for (int i = 0; i < num_frames; ++i) {
470-
if (std::find(nonsilence_frames.begin(),
471-
nonsilence_frames.end(), i / 3) == nonsilence_frames.end()) {
472+
if (nonsilence_set.find(i / 3) == nonsilence_set.end()) {
472473
continue;
473474
}
474475

0 commit comments

Comments
 (0)