On my testing regular linux box(kernel 3.4), it runs very fast. But when I move my program to another linux box(kernel 3.0), this function runs very slow, it takes about 2.5ms to return, this is like 500x slower, which causes a big performance problem.
So what might be the possible reason for such a big difference?
I don’t know why it’s so much slower on your other machine running Linux 3.0; there are many possible reasons, such as a bug in the kernel or some slow hardware. You could determine this empirically. First, see if the issue is the kernel version; install the newer kernel on the machine on which it’s slow, and see if that fixes it. If you can’t do that, you could try installing the old kernel on your dev machine. If it turns out to be based on the kernel version, then you can bisect the kernel history to figure out which commit sped it up so much. See
git bisectfor details of how to do the bisection. If it’s not a kernel problem, it’s likely a hardware issue.Regardless of what introduced the bug, you are going to want to fix it. If you can’t fix it by upgrading your kernel, you can work around it by calling
if_nameindex()once, storing the result, and looking up the result in that array. Scanning an array of around 15 items linearly should be pretty fast; much faster than 2.5 ms.