Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Absolute need of goto 4 correct functioning of loop routine.
#21
Hi, Mango

True, you save a few lines of code by that GOTO, but if you insert them as I have done below, I think the program is a bit easier to follow and it doesn't take any longer to run.

Mac

P.S. Both versions don't list "2", the first prime number. LOL.

Code:
DIM Max AS INTEGER: Max = 3500
DIM SmallPrimes(Max) AS INTEGER
CLS
PRINT "This program sticks primes in an array then prints 'em"
' set initial values
DIM a AS INTEGER: a = 0
SmallPrimes(1) = 3
DIM Next1 AS INTEGER: Next1 = 5
DIM Count AS INTEGER: Count = 1
DIM SQroot AS INTEGER
' find first 3500 primes and put them in array, smallprimes()
' use 2-byte integer for small primes
DO WHILE Count < Max
  SQroot = SQR(Next1)
  Prime = -1: ' Assume prime number
  DO
    a = a + 1
    IF Next1 MOD SmallPrimes(a) = 0 THEN Prime = 0: EXIT DO
  LOOP WHILE SQroot > SmallPrimes(a)
  IF Prime THEN Count = Count + 1: SmallPrimes(Count) = Next1
  Next1 = Next1 + 2
  a = 0
LOOP
FOR z = 1 TO 20: :  PRINT SmallPrimes(z); : NEXT z: PRINT
FOR z = Max - 20 TO Max: PRINT SmallPrimes(z); : NEXT z: PRINT
SYSTEM
Reply
#22
Hi mac. Thanks for the reply.

I don't think the goto has any value if all it does is reduce the number of lines of code...but if it saves a comparison (1 less if statement) then it has value.

finding the first 3500 primes is near instantaneous...but if you use that short list (note it fills all the 16-bit primes) to find long int (32-bit primes) primes, then skipping that extra comparison when you already know the answer saves time.

Cheers
Reply
#23
Good pont, Mango.

It's been a long time since I have considered efficieny. I was a mainframe programmer years ago who would spend days trying to eliminate one instruction. Now I am spoiled by zillion MHZ computers and simply had forgotten that instructions take time. Even now, I wonder how many seconds it would save in an actual case. But too lazy to experiment.

By the way, if you want to see some work by me on modularity, go here:
http://www.network54.com/Forum/message?f...1108145374

Cheers

Mac
Reply
#24
I decided to test after all. I used pure DOS so there were no Windows interruptions and tested with GOTO and EXIT SUB.

GOTO took 378 seconds.
EXIT SUB took 580 seconds

You were right as rain! GOTO can save significant time.

I made a post on your behalf here:

http://www.network54.com/Forum/message?f...1108231683

Mac
Reply
#25
Hey Mac...nice test. This little bit of code is just about the only place I've used a goto in a LONG time. I revisited a prime finder I made way back about 2 yrs ago while learning c++...wanted to see speed difference compared to QB...and there was that ugly goto...lingering.

Not particularly interesting, but it's a good way to eat clocks, and thus compare compiler efficiency, and let me get a handle on the impact of juggling efficiency vs code readability.

In c++, I've found that if you use good algorithms and clean code, and an optimizing compiler, the little tweaks that I'm often tempted to test in QB make no significant difference.

Cheers, and thanks for the input.

Joe
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)