Page 1 of 1

Speed problems

Posted: Thu Sep 15, 2005 5:14 am
by RayBritton
Hi, in my game i have this:

Code: Select all

do
a=a+1
key$=inkey$
if a = 100000
loop while key$=""
on a 133Mhz this takes about 1-2 min but on 1.4Ghz it takes 10sec
can someone help please

Posted: Thu Sep 15, 2005 6:34 am
by Seb McClouth
Speed of the processors does matter. And Timer doesn't work correct because of that.

There is a way around it but it takes the RealTime Clock... kinda hard to explain... and too much code anyway...

Wait for the other experts...

grtz
Seb

Posted: Thu Sep 15, 2005 10:58 am
by RayBritton
ok experts please help me

Posted: Thu Sep 15, 2005 1:51 pm
by moneo
REGARDING YOUR CODE:
* In order to count up to 100,000, the variable "a" must be either SINGLE or DOUBLE, by default or explicitly defined.

* The variable "a" must be initialized to zero before entering the DO loop.

* The statement "if a = 100000" is incomplete and invalid.

* The way your code is written, assuming the invalid statement allows it to run, the program will sit and wait for a character before exiting the DO loop. Therefore, the speed of the human hitting the key will determine how long the DO loop runs.

When you fix the code, there are other factors which will influence the speed besides the clock speed of the machines:
* Operating system (Native DOS or Windows)
* Language version (GWBasic, Basica, QuickBasic, QBasic)
* Running interpreted or compiled

SUGGESTION: It's ok to use your old 133mhz machine for testing, but if your 1.4ghz machine is where you want the final program to run, then do performance testing on the new machine and forget about it on the old machine, whether it was better or worse.
*****

Posted: Thu Sep 15, 2005 3:02 pm
by RayBritton
rights the correct code
a isn't declared as a specfic varible i just typed
a=0

Code: Select all

do
a=a+1
key$=inkey$
if a = 100000 then goto finalselection else
loop while key$=""
the a=0 isn't anywhere near is bit of code

1.4Ghz isn't my machine is a customer(ish)'s machine

i'm using
* Operating system - Windows 98SE
* Language version - QB7.1
* Running interpreted or compiled -- sorry i didn't understand this one

Posted: Thu Sep 15, 2005 4:41 pm
by moneo
RayBritton wrote:rights the correct code
a isn't declared as a specfic varible i just typed
a=0

Code: Select all

do
a=a+1
key$=inkey$
if a = 100000 then goto finalselection else
loop while key$=""
the a=0 isn't anywhere near is bit of code

1.4Ghz isn't my machine is a customer(ish)'s machine

i'm using
* Operating system - Windows 98SE
* Language version - QB7.1
* Running interpreted or compiled -- sorry i didn't understand this one
Fine, then assuming you have a label elsewhere in the program called "finalselection", this code should work.

Just a suggestion, the following line of code:
if a = 100000 then goto finalselection else
is valid and works, but in my opinion is poor programming practice. Remove the "else", it doesn't do anything and adds confusion.

When you work with QB, you have the option of running the program in interpretive mode (which is probably what you use), or you can request QB to compile the program and produce an executable file, a .exe file. You can give this .exe file to someone else and he can run your program without having to have QB.
*****

Posted: Fri Sep 16, 2005 1:10 am
by RayBritton
no i compile the program because other might not have qb

Posted: Fri Sep 16, 2005 11:00 am
by RayBritton
unforunatly i will be gone for a few days

Posted: Fri Sep 16, 2005 10:16 pm
by Michael Calkins
* In order to count up to 100,000, the variable "a" must be either SINGLE or DOUBLE, by default or explicitly defined.
You are forgetting LONGs.
a isn't declared as a specfic varible i just typed
a=0
Please put

Code: Select all

DIM a AS LONG
before you use the variable a. Otherwise, the variable is probably a SINGLE, which is a 32 bit floating point number. LONG is a 32 bit integer number, and should be much faster and much more efficient. That alone should speed your program up.
no i compile the program because other might not have qb
Anyone can freely and legally download the QBASIC Interpreter (version 1.1) from:
ftp://ftp.microsoft.com/Products/Window ... olddos.exe
Regards,
Michael

Posted: Fri Sep 16, 2005 11:05 pm
by moneo
You're right, Michael. I did forget LONG.
I stand corrected --- thanks.
*****

Posted: Fri Sep 16, 2005 11:10 pm
by Michael Calkins
You're welcome. That happens to everyone.
Regards,
Michael

Posted: Sat Sep 17, 2005 2:24 am
by RayBritton
i don't want to speed it up, i want to replace a=a+1 with something that will work indepent of the speed of the cpu

Posted: Sat Sep 17, 2005 9:30 am
by Patz QuickBASIC Creations
RayBritton wrote:i don't want to speed it up, i want to replace a=a+1 with something that will work indepent of the speed of the cpu
Try this... Using the TIMER function will do this.

Code: Select all

'PQBC - Inkey$ Wait
'---------
do 
a=a+1 
red& = timer + (how ever long suits your program as a delay)
WHILE RED& > TIMER OR key$ <> ""
key$=inkey$
WEND
if a = 100000 then gosub finalselection
loop while key$="" 
Although with this, you may want to change your "if a = 100000 then gosub finalselection" to something shorter or the program will take forever to run.

Posted: Sat Sep 17, 2005 12:22 pm
by moneo
Watch out, the Timer gets reset at midnight.
*****

Posted: Sat Sep 17, 2005 12:47 pm
by Patz QuickBASIC Creations
Good point.
This has the fix included:

Code: Select all

'PQBC - Inkey$ Wait 
'--------- 
do 
a=a+1 
red& = timer + (how ever long suits your program as a delay) 
WHILE RED& > TIMER OR key$ <> "" 
IF TIMER < 2 THEN
LET RED& = RED& - 86399
END IF
key$=inkey$ 
WEND 
if a = 100000 then gosub finalselection 
loop while key$="" 
This should work. Just make sure not to start your program at 12:00:01 AM :P

Posted: Sun Sep 18, 2005 11:48 pm
by Michael Calkins
not to nitpick, but I have 2 suggestions.
make red a floating point instead of a LONG.
and subtract 86400 instead of 86399

Regards,
Michael