Skip to content

Commit cc94952

Browse files
committed
Slight optimizations for mbest_search() and mbest_insert().
1 parent dcf7d67 commit cc94952

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/mbest.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,16 @@ void mbest_destroy(struct MBEST *mbest) {
7575
\*---------------------------------------------------------------------------*/
7676

7777
void mbest_insert(struct MBEST *mbest, int index[], float error) {
78-
int i, j, found;
78+
int i, found;
7979
struct MBEST_LIST *list = mbest->list;
8080
int entries = mbest->entries;
8181

8282
found = 0;
8383
for(i=0; i<entries && !found; i++)
8484
if (error < list[i].error) {
8585
found = 1;
86-
for(j=entries-1; j>i; j--)
87-
list[j] = list[j-1];
88-
for(j=0; j<MBEST_STAGES; j++)
89-
list[i].index[j] = index[j];
86+
memmove(&list[i+1], &list[i], sizeof(struct MBEST_LIST) * (entries - i - 1));
87+
memcpy(&list[i].index[0], &index[0], sizeof(int) * MBEST_STAGES);
9088
list[i].error = error;
9189
}
9290
}
@@ -124,17 +122,21 @@ void mbest_search(
124122
)
125123
{
126124
float e;
127-
int i,j;
128-
float diff;
125+
int j;
129126

130127
for(j=0; j<m; j++) {
128+
float diff;
129+
int i;
130+
131131
e = 0.0;
132132
for(i=0; i<k; i++) {
133133
diff = cb[j*k+i]-vec[i];
134134
e += diff*w[i]*diff*w[i];
135135
}
136+
136137
index[0] = j;
137-
mbest_insert(mbest, index, e);
138+
if (e < mbest->list[mbest->entries - 1].error)
139+
mbest_insert(mbest, index, e);
138140
}
139141
}
140142

0 commit comments

Comments
 (0)