title: Most efficient way to calculate CRC16 in Python
body: <p>I need to calculate CRC16 of binary numbers several times inside a loop. I have used the following method</p>
  
  <pre><code>import numpy as np
  import binascii
  #I have just filled the array with random numbers
  #These arrays are loaded from a file
  array1=np.random.randint(0,511, size=100000)
  array2=np.random.randint(0,511, size=100000)
  #...
  #This goes on to till say array100
  #Now calculate crc of each row in a loop
  for j in range(100000):
      crc=0xffff
      #Convert the number to binary 16 bit format
      temp_bin=np.binary_repr(array1[j], 16)
      crc=binascii.crc_hqx(chr(int(temp_bin[0:8],2)), crc)
      crc=binascii.crc_hqx(chr(int(temp_bin[8:16],2)), crc)
      #Similarly for array2
      temp_bin=np.binary_repr(array2[j], 16)
      crc=binascii.crc_hqx(chr(int(temp_bin[0:8],2)), crc)
      crc=binascii.crc_hqx(chr(int(temp_bin[8:16],2)), crc)
      #...
      #This goes on till array100
  </code></pre>
  
  <p>While this method works perfectly, it is extremely slow. On profiling, I find converting each number to binary is the major bottleneck in my code.</p>
  
  <p>Total time: 10.9712 s</p>
  
  <p>File: speedup.py</p>
  
  <p>Function: abc at line 7</p>
  
  <p>Line _____       Hits  ____       Time  ____Per Hit ____  % Time  ____ Line Contents</p>
  
  <pre><code> 7                                           @profile
   8                                           def abc():
   9                                               #I have just filled the array with random numbers
  10                                               #Thse arrays are loaded from a file
  11         1       3269.0   3269.0      0.0      array1=np.random.randint(0,511, size=100000)
  12         1       3206.0   3206.0      0.0      array2=np.random.randint(0,511, size=100000)
  13                                               #...
  14                                               #This goes on to till say array100
  15                                               #Now calculate crc of each row in a loop
  16    100001     237461.0      2.4      2.2      for j in range(100000):
  17    100000     199887.0      2.0      1.8          crc=0xffff
  18                                                   #Convert the number to binary 16 bit format
  19    100000    3436116.0     34.4     31.3          temp_bin=np.binary_repr(array1[j], 16)
  20    100000    1039049.0     10.4      9.5          crc=binascii.crc_hqx(chr(int(temp_bin[0:8],2)), crc)
  21    100000     793751.0      7.9      7.2          crc=binascii.crc_hqx(chr(int(temp_bin[8:16],2)), crc)
  22                                                   ##Similarly for array2
  23    100000    3423862.0     34.2     31.2          temp_bin=np.binary_repr(array2[j], 16)
  24    100000     991331.0      9.9      9.0          crc=binascii.crc_hqx(chr(int(temp_bin[0:8],2)), crc)
  25    100000     843271.0      8.4      7.7          crc=binascii.crc_hqx(chr(int(temp_bin[8:16],2)), crc)
  </code></pre>
  
  <p>I have not been able to come up with an alternative solution that avoids it. So is there a more efficient and pythonic way to convert numbers to binary or to do this entire thing?</p>