Why is this function so slow?

Why is this function so slow?