Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
***ATTENTION OPTIMIZERS***
#1
So...I don't know much about using bitwise operators as substitutes for normal math. This code has several "divide by 2's" in it. Does anyone have a good way of speeding things up? On my PC, a ~ 2MB file takes about 2 sec...seeing as I need the number for further manipulations, I'd like to minimize this time. I don't believe that this code correctly calculates the CRC32...however, it does give a pseudo-unique 4-byte number for any file...a number that will change with any change to the file...which is what I am after.

Any help speeding things up, or alternate code that will *correctly* calculate the CRC32 for a file would be appreciated.

Code:
'I adapted this code from: http://pbsound.basicguru.com/files/pbsource/crc32bas.bas


DEFINT A-Z

FILES: INPUT "file to crc"; p$  'use this line to get user input for filename
'p$ = COMMAND$   'use this line if compiled (drag-n-drop) (and comment out the preceeding line)

PRINT "calculating CRC for "; p$

DIM CRCTable(256) AS LONG
DIM Buffer AS STRING * 4096

FOR I = 0 TO 255  'generate table
  CRC32& = I
  FOR J = 1 TO 8
    IF (CRC32& AND 1) THEN
      CRC32& = (CRC32& \ 2) XOR -306674912
    ELSE
      CRC32& = CRC32& \ 2
    END IF
  NEXT J
  CRCTable&(I) = CRC32&
NEXT I


OPEN p$ FOR BINARY AS 1
filelen& = LOF(1)
CRC32& = -1

DO UNTIL EOF(1)  'calculate unique number for file
  GET #1, , Buffer$
  FOR a% = 1 TO LEN(Buffer$)
     TEMP1& = CRC32& \ 256
     TEMP2& = CRCTable&((CRC32& XOR ASC(MID$(Buffer$, a%, 1))) AND 255)
     CRC32& = TEMP1& XOR TEMP2&
  NEXT
LOOP

CRC32& = CRC32& XOR -1
CLOSE #1

CLS
PRINT "the file "; p$; " is "; filelen& / 1000; " kb and has a crc of (dec/hex):"
PRINT CRC32&, HEX$(CRC32&)

DO
LOOP UNTIL INKEY$ <> ""
Reply


Messages In This Thread
***ATTENTION OPTIMIZERS*** - by Mango - 05-17-2003, 03:22 AM
***ATTENTION OPTIMIZERS*** - by na_th_an - 05-17-2003, 06:26 AM
***ATTENTION OPTIMIZERS*** - by Plasma - 05-17-2003, 06:27 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)