Qbasicnews.com
February 27, 2020, 08:14:24 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1] 2 3
  Print  
Author Topic: Code optimization tips and stuff.  (Read 6566 times)
Rokkuman
Na_th_an
*****
Posts: 1973



« on: May 15, 2003, 12:29:38 AM »

Ok, I just thought this would be a good place to discuss techniques that we\you all use to optomize your code and use less memory and make your games run faster, ect... and this is assuming you're not using libs.
Logged
wizardlife
Na_th_an
*****
Posts: 1456


WWW
« Reply #1 on: May 15, 2003, 12:47:23 AM »

Quote from: "Megaman"
Ok, I just thought this would be a good place to discuss techniques that we\you all use to optomize your code and use less memory and make your games run faster, ect... and this is assuming you're not using libs.


I've never really done anything outside of common sense:

- Avoid recursion
- Avoid a lot of nested loops
- Avoid division (especially on floats)
- Strongly type variables
- Be especially careful of efficiency in routines that are executed many times per frame (drawing, particle updates, per-pixel effects, etc...)
Logged

Mango
Wandering Guru
***
Posts: 360



« Reply #2 on: May 15, 2003, 01:01:03 AM »

Quote from: "Megaman"
Ok, I just thought this would be a good place to discuss techniques that we\you all use to optomize your code and use less memory and make your games run faster, ect... and this is assuming you're not using libs.


I have a lot to learn in this area...and hope to get some pointers from this thread.  However...here are my contributions

1-use short integers when you can
2-use long integers when shorts don't work
3-use integer division and MOD instead of 'normal' division
4-use x*x instead of X^2
5-test to see what works faster
6-compile...non-compiled code gets slowed by comments & spaces
7-when the speed of a process doesn't matter...write for ease of understanding rather than speed.  eg, if you want to reverse the order of a short string that was input by the user...don't worry about optimizing.  It will be done in a flash regardless of the code.  However, if you are converting bytes to long integers as a main function of the program, then optimize with abandon.  However, always keep the end goal (and how long it will take to achieve) in mind when coding.
8-when accessing files in BINARY mode, use the largest data-type you can.  eg, PUT data as Long Integer (4-bytes) rather than as single bytes (String * 1)...or write block strings...however...if you take your 32-bit computer and write 8-bits at a time....speed will suffer.
5-when you don't know what will be faster, test it....eg...is a FOR/NEXT  loop faster than a DO/LOOP???  Find out!!!

Code:

deflng a-z
t=timer
for x=1 to 2000000000
if timer-t>1 then exit for
next x
print "in one sec, FOR/NEXT gave";x


x=0
t=timer
do
x=x+1
if timer-t>1 then exit do
loop
print "in one sec, DO/LOOP gave ";x
Logged
toonski84
__/--\__
*****
Posts: 2567



« Reply #3 on: May 15, 2003, 01:21:34 AM »

um, dont mix data types and use bit operators when applicable?  y'know, most of these tips have been posted in qb magazines like qbcm and tm.
Logged

i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Blitz
I hold this place together
*****
Posts: 853



WWW
« Reply #4 on: May 15, 2003, 05:38:43 AM »

I just had to post here, cuz these tips aren't any good.
Here's how you really optimize

You time each section of the code to find out where most of the time is spent.

Once you've found that/those part(s) the best optimization you can do is to use a better algorithm. It doesn't matter how many details you change, if the algo suxs it won't matter. So in other words, real speed is in algos and that only.

- In the time critical parts follow these rules
- avoid division of any sort
- avoid longs
- Do use floating point when you need it, on a pentium or better it's as fast as integer math. And use ffix.
- DO NOT USE FIXED POINT, becuase of rule one. Qb converts multiplications by powers of 2 to bit shifts but for division it uses the division instruction which is very very slow. And also becuase of rule 3, longs are emulated in software by qb. very very slow.
- Do not mix integer and floating point all the time. Converting to and from float/int isn't slow. But it could be faster and this is something you want to avoid unless you need it. Therefor use integer division ( \ ) in integer math and float division ( / ) in floating point math and so on.
- The mod instruction uses division, so you should also avoid it. However, when you need to mod by powers of 2 lets say 64, you can do a and 63 instead. Much faster.

This is what i could think of right now, anywhere else in the code that's not time critical use anything you need, whenever you need it.
Logged

oship me and i will give you lots of guurrls and beeea
LooseCaboose
I hold this place together
*****
Posts: 981



« Reply #5 on: May 15, 2003, 07:58:53 AM »

In addition to needing good algorithms (for once I agree with you Blitz Shocked) good data structures can also help provide a speed boost. Sorted lists and binary trees make lookup on large amounts of data much faster than linear searching for example. Reading/writing chunks of data is usually much faster than several small accesses. If you are storing large structures in memory, grab the whole lot in one go, instead of reading each individual entity separately.
Logged

esus saves.... Passes to Moses, shoots, he scores!
Antoni Gual
Na_th_an
*****
Posts: 1434



WWW
« Reply #6 on: May 15, 2003, 09:55:23 AM »

A tutorial by Toshi Horie on speed optimization..
http://www.ocf.berkeley.edu/~horie/basopt.html

I could add my own findings
Never call a sub/function in a different module in a critical loop.

On strings (not covered in Toshi's tutorial):
Avoid changing the length of the strings.
Use MID$  , LSET,RSET instead of direct assign or string concat.
Use PEEK Instead of ASC(MID$()).

And don't trust the tutorials, benchmark yourself all optimizations.
Logged

Antoni
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #7 on: May 15, 2003, 11:20:32 AM »

Blitz, you are quite wrong on the mod division point.

Code:

'CLS
b% = 10
t1# = TIMER
FOR I# = 1 TO 10000000
a% = b% MOD 5
NEXT I#
t2# = TIMER - t1#
PRINT t2#


Changing mod to \ actually makes it a bit slower.
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #8 on: May 15, 2003, 04:32:05 PM »

Mod and \ are not the same operation. Furthermore, he told to use ffix. Didn't he?
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Glenn
I hold this place together
*****
Posts: 786



WWW
« Reply #9 on: May 15, 2003, 04:46:05 PM »

the same thing.  It would have to be

A MOD B = (A / B -  (A \ B)) * B
Logged

ravelling Curmudgeon
(geocities sites require copying and pasting URLs.)
I liked spam better when it was something that came in a can.
Windows should be defenestrated.
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #10 on: May 15, 2003, 04:47:34 PM »

Quote
Mod and \ are not the same operation.


Oh, well in that case, mod is still faster! So ignore Blitz and use mod!

Ffix doesn't apply to integers... does it?
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Glenn
I hold this place together
*****
Posts: 786



WWW
« Reply #11 on: May 15, 2003, 04:51:12 PM »

MOD and "\" are the same thing?  (Are my glasses *that* bad?)
Logged

ravelling Curmudgeon
(geocities sites require copying and pasting URLs.)
I liked spam better when it was something that came in a can.
Windows should be defenestrated.
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #12 on: May 15, 2003, 05:00:45 PM »

he didn't. I thought he did.


but he did say:
Quote

The mod instruction uses division, so you should also avoid it.
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Blitz
I hold this place together
*****
Posts: 853



WWW
« Reply #13 on: May 15, 2003, 05:03:43 PM »

Christ, is my english taht bad?
I said MOD USES division, i never said replace mod with division. I said replace mod which are powers of to with AND. Otherwise avoid it in time critical parts.

I didn't even have to write code to know that it's correct, but since you seem keen on me being wrong here.

MODs per second: 37577982
ANDs per second: 390095238
AND is percent faster: 1038

Code:

defint a-z

dim a as integer
dim i as integer, j as integer
dim tmrAIni as double, tmrBIni as double
dim tmrAEnd as double, tmrBEnd as double
dim modspersec as double, andspersec as double

tmrAIni = timer
for  i = 0 to 199
    for  j = 0 to 31999
        a = j mod 2
        a = j mod 4
        a = j mod 8
        a = j mod 16
        a = j mod 32
        a = j mod 64
        a = j mod 128
        a = j mod 256
        a = j mod 512
        a = j mod 1024
    next j
next i
tmrAEnd = timer

tmrBIni = timer
for  i = 0 to 199
    for  j = 0 to 31999
        a = j and 1
        a = j and 3
        a = j and 7
        a = j and 15
        a = j and 31
        a = j and 63
        a = j and 127
        a = j and 255
        a = j and 511
        a = j and 1023
    next j
next i
tmrBEnd = timer


modspersec = (32000.0#*200.0#*10.0#) / (tmrAEnd-tmrAIni)
andspersec = (32000.0#*200.0#*10.0#) / (tmrBEnd-tmrBIni)

print "MODs per second:" + str$( clng( modspersec ) )
print "ANDs per second:" + str$( clng( andspersec ) )
print "AND is percent faster:" + str$( cint( andspersec*100.0#/modspersec ) )
Logged

oship me and i will give you lots of guurrls and beeea
DrV
Na_th_an
*****
Posts: 1553



WWW
« Reply #14 on: May 15, 2003, 08:59:36 PM »

You said use / for integer division and \ for float division.  That's backwards.  \ chops off the fractional part of a division.
Logged
Pages: [1] 2 3
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!