Qbasicnews.com
May 26, 2019, 11:15:27 AM
 Welcome, Guest. Please login or register. 1 Hour 1 Day 1 Week 1 Month Forever Login with username, password and session length
 Home Help Search Login Register
 Pages: [1]
 Author Topic: ***ATTENTION OPTIMIZERS***  (Read 1736 times)
Mango
Wandering Guru

Posts: 360

 « on: May 16, 2003, 07:22:57 PM »

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\$ <> ""
 Logged
na_th_an
*/-\*

Posts: 8244

 « Reply #1 on: May 16, 2003, 10:26:02 PM »

Multiplications and divisions by powers of two can be substituted by shifts left or right (respectively). In QB there is no shift division but many people in this forum state that BC.EXE translates such multiplications/divisions by their correspondent shift, so it is right to use \2, \4, \8 and so on.
 Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Plasma
Na_th_an

Posts: 1770

 « Reply #2 on: May 16, 2003, 10:27:22 PM »

Read the characters in the buffer directly with PEEK instead of ASC(MID\$())
 Logged
 Pages: [1]