Created
September 9, 2023 16:26
-
-
Save handymenny/4d120f81877075731eb0511f9af30578 to your computer and use it in GitHub Desktop.
Benchmark for CRC16 x25/IBM-SDLC/ISO-HDLC Python libraries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Benchmark for CRC16 x25/IBM-SDLC/ISO-HDLC libraries | |
import crccheck.crc | |
import fastcrc | |
import scat.util | |
import libscrc | |
import crcelk | |
import timeit | |
import random | |
def testDmCrc16(iterations, size): | |
random_bytes = random.randbytes(size) | |
for i in range(iterations): | |
scat.util.dm_crc16(random_bytes) | |
def testCrcElk(iterations, size): | |
random_bytes = random.randbytes(size) | |
for i in range(iterations): | |
crcelk.CRC_HDLC.calc_bytes(random_bytes) | |
def testCrcCheck(iterations, size): | |
random_bytes = random.randbytes(size) | |
for i in range(iterations): | |
crccheck.crc.Crc16X25.calc(random_bytes) | |
def testLibscrc(iterations, size): | |
random_bytes = random.randbytes(size) | |
for i in range(iterations): | |
libscrc.x25(random_bytes) | |
def testFastCrc(iterations, size): | |
random_bytes = random.randbytes(size) | |
for i in range(iterations): | |
fastcrc.crc16.ibm_sdlc(random_bytes) | |
def self_test(): | |
input = b'123456789' | |
oracle = 0x906E | |
crc_fromDmCrc16 = scat.util.dm_crc16(input) | |
crc_fromCrcElk = crcelk.CRC_HDLC.calc_bytes(input) | |
crc_fromCrcCheck = crccheck.crc.Crc16IsoHdlc.calc(input) | |
crc_fromLibscrc = libscrc.x25(input) | |
crc_fromFastCrc = fastcrc.crc16.ibm_sdlc(input) | |
assert crc_fromDmCrc16 == oracle | |
assert crc_fromCrcElk == oracle | |
assert crc_fromCrcCheck == oracle | |
assert crc_fromLibscrc == oracle | |
assert crc_fromFastCrc == oracle | |
self_test() | |
sizes = [8, 64, 512, 4096] | |
# Use iterations > 1 to reduce random.randbytes overhead | |
iterations = 100 | |
number = 1000 | |
for i in sizes: | |
print(f'{iterations} * {number} iterations, size {i}') | |
elapsedTime = timeit.timeit( | |
f"testDmCrc16({iterations}, {i})", number=number, globals=locals()) | |
print(f'dm_crc16: {round(elapsedTime * 1000)}ms') | |
if (i < 512): | |
# crcelk and crc_check are too slow | |
elapsedTime = timeit.timeit( | |
f"testCrcElk({iterations}, {i})", number=number, globals=locals()) | |
print(f'crcelk: {round(elapsedTime * 1000)}ms') | |
elapsedTime = timeit.timeit( | |
f"testCrcCheck({iterations}, {i})", number=number, globals=locals()) | |
print(f'crccheck: {round(elapsedTime * 1000)}ms') | |
elapsedTime = timeit.timeit( | |
f"testLibscrc({iterations}, {i})", number=number, globals=locals()) | |
print(f'libscrc: {round(elapsedTime * 1000)}ms') | |
elapsedTime = timeit.timeit( | |
f"testFastCrc({iterations}, {i})", number=number, globals=locals()) | |
print(f'fastcrc: {round(elapsedTime * 1000)}ms') | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment