Speed problems

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
RayBritton
Veteran
Posts: 109
Joined: Thu Feb 10, 2005 12:38 pm

Speed problems

Post 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
Seb McClouth

Post 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
RayBritton
Veteran
Posts: 109
Joined: Thu Feb 10, 2005 12:38 pm

Post by RayBritton »

ok experts please help me
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post 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.
*****
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.
RayBritton
Veteran
Posts: 109
Joined: Thu Feb 10, 2005 12:38 pm

Post 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
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post 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.
*****
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.
RayBritton
Veteran
Posts: 109
Joined: Thu Feb 10, 2005 12:38 pm

Post by RayBritton »

no i compile the program because other might not have qb
RayBritton
Veteran
Posts: 109
Joined: Thu Feb 10, 2005 12:38 pm

Post by RayBritton »

unforunatly i will be gone for a few days
User avatar
Michael Calkins
Veteran
Posts: 76
Joined: Tue Apr 05, 2005 8:40 pm
Location: Floresville, Texas
Contact:

Post 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
Bring on the Maulotaurs! oops...
I like to slay Disciples of D'Sparil...
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

You're right, Michael. I did forget LONG.
I stand corrected --- thanks.
*****
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.
User avatar
Michael Calkins
Veteran
Posts: 76
Joined: Tue Apr 05, 2005 8:40 pm
Location: Floresville, Texas
Contact:

Post by Michael Calkins »

You're welcome. That happens to everyone.
Regards,
Michael
Bring on the Maulotaurs! oops...
I like to slay Disciples of D'Sparil...
RayBritton
Veteran
Posts: 109
Joined: Thu Feb 10, 2005 12:38 pm

Post 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
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post 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.
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Watch out, the Timer gets reset at midnight.
*****
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post 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
User avatar
Michael Calkins
Veteran
Posts: 76
Joined: Tue Apr 05, 2005 8:40 pm
Location: Floresville, Texas
Contact:

Post 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
Bring on the Maulotaurs! oops...
I like to slay Disciples of D'Sparil...
Post Reply