1
아래 코드는 "REPLACING WITH .."행 근처에서 functor compare_swaps()가 my_intcmp()에 대한 직접 참조로 바뀌면 4 배 빠르게 실행됩니다. 명백하게 간접 사용은 인라인되지 않습니다. 왜?템플릿 함수 + 펑터 인수, 펑터가 인라인되지 않은 이유는 무엇입니까?
inline bool my_intcmp(const int & a, const int & b) {
return a < b;
}
template <class T, class compare>
void insertion_sort_3(T* first, T* last, compare compare_swaps) {
// Count is 1?
if(1 == (last - first))
return;
for(T* it = first + 1; it != last; it ++) {
T val = *it;
T* jt;
for(jt = it; jt != first; jt --) {
// REPLACING WITH if(my_intcmp(val, *(jt - 1)) gives 4x speedup, WHY?
if(compare_swaps(val, *(jt - 1))) {
*jt = *(jt - 1);
} else {
break;
}
}
*jt = val;
}
}
#define SIZE 100000
#define COUNT 4
int main() {
int myarr[ SIZE ];
srand(time(NULL));
int n = COUNT;
while(n--) {
for(int i = 0; i < SIZE; i ++)
myarr[ i ] = rand() % 20000;
insertion_sort_3(myarr, myarr + SIZE, my_intcmp);
}
return 0;
}
컴파일러는 무엇입니까? – Jagannath
BTW, 빠른 정렬 알고리즘을 원한다면 삽입 정렬보다 효과적인 알고리즘을 사용하고 싶을 것입니다. –
"* functor가 인라인되지 않는 이유 * *"- my_intcmp는 Functor가 아니기 때문에. –