1

I have function to calculate STM crc32 of bytes array. The problem is that for 5120 byte array it takes 20ms which is definitely too long. There is an option to improve speed of this code, it can max takes about 5ms??

code :

def crc32_stm(bytes_arr):

    length = len(bytes_arr)
    crc = 0xffffffff

    k = 0
    while length >= 4:

        v = ((bytes_arr[k] << 24) & 0xFF000000) | ((bytes_arr[k+1] << 16) & 0xFF0000) | \
        ((bytes_arr[k+2] << 8) & 0xFF00) | (bytes_arr[k+3] & 0xFF)

        crc = ((crc << 8) & 0xffffffff) ^ custom_crc_table[0xFF & ((crc >> 24) ^ v)]
        crc = ((crc << 8) & 0xffffffff) ^ custom_crc_table[0xFF & ((crc >> 24) ^ (v >> 8))]
        crc = ((crc << 8) & 0xffffffff) ^ custom_crc_table[0xFF & ((crc >> 24) ^ (v >> 16))]
        crc = ((crc << 8) & 0xffffffff) ^ custom_crc_table[0xFF & ((crc >> 24) ^ (v >> 24))]

        k += 4
        length -= 4

    if length > 0:
        v = 0

        for i in range(length):
            v |= (bytes_arr[k+i] << 24-i*8)

        if length == 1:
            v &= 0xFF000000

        elif length == 2:
            v &= 0xFFFF0000

        elif length == 3:
            v &= 0xFFFFFF00

        crc = (( crc << 8 ) & 0xffffffff) ^ custom_crc_table[0xFF & ( (crc >> 24) ^ (v ) )];
        crc = (( crc << 8 ) & 0xffffffff) ^ custom_crc_table[0xFF & ( (crc >> 24) ^ (v >> 8) )];
        crc = (( crc << 8 ) & 0xffffffff) ^ custom_crc_table[0xFF & ( (crc >> 24) ^ (v >> 16) )];
        crc = (( crc << 8 ) & 0xffffffff) ^ custom_crc_table[0xFF & ( (crc >> 24) ^ (v >> 24) )];

    global stmCrc
    stmCrc = crc
    return crc
KyluAce
  • 933
  • 1
  • 8
  • 25
  • have you tried using the `binascii.crc32` function? not sure if you want signed or unsigned, that function gives signed, and to turn to unsigned: `crc32 = binascii.crc32(data) % (1<<32)` – James Kent Sep 07 '17 at 10:51
  • binascii give just crc not stm crc so I can't use binascii or zlib – KyluAce Sep 07 '17 at 10:56
  • What's in the table? – Mark Adler Sep 08 '17 at 06:19
  • If your code is already working, consider posting on [Code Review.SE](https://codereview.meta.stackexchange.com/questions/5777/a-guide-to-code-review-for-stack-overflow-users) – jtbandes Sep 08 '17 at 06:23
  • STM chips can be configured for the usual zlib CRC32 (as opposed to the default MPEG2 CRC32). More information in https://stackoverflow.com/questions/28064278/matching-crc32-from-stm32f0-and-zlib – Yann Vernier Sep 08 '17 at 06:42

1 Answers1

2

You can use crcmod to generate C code that you compile and then call from Python. That will be orders of magnitude faster than the Python code.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158