>>51098832>do i need to go benchmark qsort v. std::sort again. this is like an infamous problem>short version: qsort can't inline the comparator unless qsort is inlined.That's because qsort is typically compiled code that's dynamically linked in, so of course it has no chance of inlining it.
But if you provide the definition of qsort in the header (like the template version does) compilers can inline the function pointers no problem:
static int add(int x, int y) { return x + y; }
static int mul(int x, int y) { return x * y; }
int fold(int *a, int n, int x, int (*fn)(int, int))
{
int i, s = x;
for (i=0; i<n; ++i)
s = fn(s, a[i]);
return s;
}
int test_add(int *a, int n)
{
return fold(a, n, 0, add);
}
int test_mul(int *a, int n)
{
return fold(a, n, 1, mul);
}
0000000000000040 <test_add>:
40: test edx,edx
42: jle 5d <test_add+0x1d>
44: sub edx,0x1
47: xor eax,eax
49: lea rdx,[rcx+rdx*4+0x4]
4e: xchg ax,ax
50: add eax,DWORD PTR [rcx]
52: add rcx,0x4
56: cmp rcx,rdx
59: jne 50 <test_add+0x10>
5b: repz ret
5d: xor eax,eax
5f: ret
0000000000000060 <test_mul>:
60: test edx,edx
62: jle 7f <test_mul+0x1f>
64: sub edx,0x1
67: mov eax,0x1
6c: lea rdx,[rcx+rdx*4+0x4]
71: imul eax,DWORD PTR [rcx]
74: add rcx,0x4
78: cmp rcx,rdx
7b: jne 71 <test_mul+0x11>
7d: repz ret
7f: mov eax,0x1
84: ret