How to speed up your QuickBMS scripts
Posted: Tue Sep 04, 2012 1:31 am
When processing large files or small portions of data in huge loops, scripts can be really slow. Often it's because of faulty code. So here are some general tips for QuickBMS coders to make your scripts faster.
This list may be updated in the future.
1. Avoid double calculation
avoid calculating variables inside a loop when you can do so outside. Example:
should be corrected to
2. Avoid appending (thanks for this tip, Luigi
)
Instead of
use
3. Pre-allocate memory
Whenever you have to write something to MEMORY_FILE, set it's size first:
If you don't know the size of the complete file without running through the data and getting single block sizes (e.g. when demultiplexing certain video formats), it's faster to pre-allocate the memory in one loop and then fill it in a second one:
1. Avoid double calculation
avoid calculating variables inside a loop when you can do so outside. Example:
Code: Select all
set HEADER 0x800
set DAT 0x100
for i = 1 <= 10000
get FSIZE asize # same operation each loop
math FSIZE -= HEADER # "
set SUB i
math SUB *= DAT
math FSIZE -= SUB
next iCode: Select all
set HEADER 0x800
set DAT 0x100
get TSIZE asize
math TSIZE -= HEADER # one calculation before the loop
for i = 1 <= 10000
set FSIZE TSIZE # just take the already calculated variable
set SUB i
math SUB *= DAT
math FSIZE -= SUB
next i2. Avoid appending (thanks for this tip, Luigi
Instead of
Code: Select all
get CYCLES asize
math CYCLES /= 8
set OFFSET 0
append
for i = 1 <= CYCLES
log MEMORY_FILE OFFSET 8
math OFFSET += 8
next i
appendCode: Select all
get CYCLES asize
math CYCLES /= 8
for i = 1 <= CYCLES
getDstring DATA 8
putDstring DATA 8 MEMORY_FILE
next i3. Pre-allocate memory
Whenever you have to write something to MEMORY_FILE, set it's size first:
Code: Select all
putVarChr MEMORY_FILE PRE_SIZE 0
log MEMORY_FILE 0 0Code: Select all
set PSIZE 0
savepos MYOFF
do
get MSIZE long
savepos OFFSET
math PSIZE += MSIZE
putVarChr MEMORY_FILE PSIZE 0
log MEMORY_FILE 0 0
while [ending argument]
goto MYOFF
do
get MSIZE long
savepos TEMPOFF
getDstring DATA MSIZE
putDstring DATA MSIZE MEMORY_FILE
math TEMPOFF += MSIZE
goto TEMPOFF
while [ending argument]