Number of comparisons made in median of 3 function?

后端 未结 6 905
轻奢々
轻奢々 2020-12-19 04:16

As of right now, my functioin finds the median of 3 numbers and sorts them, but it always makes three comparisons. I\'m thinking I can use a nested if statement somewhere so

6条回答
  •  时光说笑
    2020-12-19 04:47

    If you only need the median value, here's a branch-less solution based on min/max operators:

    median = max(min(a,b), min(max(a,b),c));

    Intel CPU's have SSE min/max vector instructions, so depending on your or your compiler's ability to vectorize, this can run extremely fast.

提交回复
热议问题