I'm not sure this algo is a good candidate for either XS or Inline::C. It's not computationally intensive. There might be a small boost in performance from going to XS, but unless this particular sub has been identified as a bottleneck during profiling and you need to squeeze every last CPU cycle out of it, I'd leave it in Perl.