RobTillaart / Fletcher

Arduino Library for calculating Fletcher's checksum
MIT License
4 stars 2 forks source link

Performance #7

Closed daniel-mohr closed 2 years ago

daniel-mohr commented 2 years ago

Since #6 gives a test of all Fletcher implementations here, I also investigated quickly the ideas written on wikipedia:Fletcher's checksum.

Using bit shifts instead of the if statement could speed up the things:

Data rate on Arduino Nano (ATmega 328P): alg if statement bit shifts
Fletcher16 1333.04 us/kByte 1301.39 us/kByte
Fletcher32 1106.78 us/kByte 1302.26 us/kByte
Fletcher64 3791.83 us/kByte 6408.00 us/kByte
Data rate on Arduino MKRZERO (SAMD21): alg if statement bit shifts
Fletcher16 1011.79 us/kByte 968.48 us/kByte
Fletcher32 597.56 us/kByte 505.77 us/kByte
Fletcher64 436.85 us/kByte 425.15 us/kByte

I believe that for 8 Bit (Fletcher16) it's always the same. Therefore I adapted your code here.

For more than 8 Bit (Fletcher32 or Fletcher64) I believe it depends on the platform. Therefore I adapted your code only for SAMD21, which I could test. This could be enhanced for all 16 Bit or 32 Bit systems.

I hope this PR is OK?

daniel-mohr commented 2 years ago

Sorry, this PR is based on #6. I hope #6 is OK and therefore also this dependency here.

RobTillaart commented 2 years ago

As it is rather late I will dive into the proposed code tomorrow. The numbers are not always an improvement, so it might need conditional code to select fastest method per platform. If AVR ..erc

RobTillaart commented 2 years ago

Looks good, merging this makes PR #6 obsolete.