Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer concatenation c

Tags:

c

I'm trying to concatenate two uint32_t and get back a uint64_t. Here's my method

uint64_t concat_int1_int2 (uint32_t int1, uint32_t int2) {
  uint64_t concatenated = int1;
  return concatenated << 32 | int2;
}

This seems to be extremely slow (I need to do it ~1,000,000 times and it is taking ~ 6 minutes). Two questions, why does the bit shift take so long (that seems to be the limiting step), and does anyone have a suggestion for a faster way to do this?

like image 612
August Flanagan Avatar asked Nov 03 '11 03:11

August Flanagan


2 Answers

The way you are doing it is correct. It may help to inline the function. Most likely your performance problem is in code you haven't shown us.

like image 188
David Schwartz Avatar answered Oct 07 '22 01:10

David Schwartz


Your solution is near optiomal, it can't be much faster. Marking the function as inline may help a very little, but will not make much difference. I made a test here in my machine using your code, it took 10ms to run 1,000,000 iterations of it. Your speed problem is somewhere else.

like image 31
lvella Avatar answered Oct 06 '22 23:10

lvella