Megalextoria
Retro computing and gaming, sci-fi books, tv and movies and other geeky stuff.

Home » Digital Archaeology » Computer Arcana » Commodore » Commodore 8-bit » C64 BASIC - what's faster INT() or %? Plus benefits of DEF FN A()
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
C64 BASIC - what's faster INT() or %? Plus benefits of DEF FN A() [message #366351] Mon, 16 April 2018 12:20 Go to next message
Anonymous
Karma:
Originally posted by: Shaun Bebbington

There seems to be two ways to do integers on the C64 (and yes I know floats are fastest), I was wondering about these two methods of generating a random positive number between 1 and 64 inclusive:

10 X=INT(64*RND(0)+1)

or

10 X%=64*RND(0)+1

The latter saves two whole bytes, but is it faster to convert to integer directly with the INT keyword, or is the implicit declaration below faster?

Second question, let's say you want to generate a random number in multiple places, you could use the above several times in a listing, but one could also define a function as I understand it, like:

0 DEF FN A(X)=X*RND(0)

So each time in your listing, you will have,

10 X%=FN A(39): Y%=FN A(24)
....
100 S%=FN A(10)+1: D%=FN A(10)+1

etc...

Other than less typing throughout the program (where you might have lots of repeated random number generation in this example), are there any benefits to using CBM functions? Does it save bytes overall would be perhaps one that I could think of.

Many thanks,

Shaun.
Re: C64 BASIC - what's faster INT() or %? Plus benefits of DEF FN A() [message #366363 is a reply to message #366351] Mon, 16 April 2018 17:08 Go to previous messageGo to next message
Whammo is currently offline  Whammo
Messages: 31
Registered: August 2012
Karma: 0
Member
10 X=INT(64*RND(0)+1)

Here, X is a defined as a float, so you're using the variable space of a float and explicitly calling the INT function.

or

10 X%=64*RND(0)+1

Here, X% is defined as an integer it uses less space and it only needs to read and write the bytes of an integer.
Re: C64 BASIC - what's faster INT() or %? Plus benefits of DEF FN A() [message #366364 is a reply to message #366351] Mon, 16 April 2018 17:20 Go to previous messageGo to next message
Whammo is currently offline  Whammo
Messages: 31
Registered: August 2012
Karma: 0
Member
> Other than less typing throughout the program (where you might have lots of repeated random number generation in this example), are there any benefits to using CBM functions? Does it save bytes overall would be perhaps one that I could think of.
>
> Many thanks,
>
> Shaun.

Other than that, it's faster to inline code than to define a function.
Re: C64 BASIC - what's faster INT() or %? Plus benefits of DEF FN A() [message #366365 is a reply to message #366364] Mon, 16 April 2018 17:29 Go to previous messageGo to next message
Anonymous
Karma:
Originally posted by: Shaun Bebbington

On Monday, 16 April 2018 22:20:51 UTC+1, Whammo wrote:
>> Other than less typing throughout the program (where you might have lots of repeated random number generation in this example), are there any benefits to using CBM functions? Does it save bytes overall would be perhaps one that I could think of.
>>
>> Many thanks,
>>
>> Shaun.
>
> Other than that, it's faster to inline code than to define a function.

Thanks for the info.
Re: C64 BASIC - what's faster INT() or %? Plus benefits of DEF FN A() [message #366396 is a reply to message #366365] Tue, 17 April 2018 08:21 Go to previous messageGo to next message
Anonymous
Karma:
Originally posted by: Shaun Bebbington

For clarification, which is faster?

10 X=INT(64*RND(0)+1)

or

10 X%=64*RND(0)+1

Thanks,

Shaun.
Re: C64 BASIC - what's faster INT() or %? Plus benefits of DEF FN A() [message #366406 is a reply to message #366396] Tue, 17 April 2018 14:49 Go to previous messageGo to next message
frank is currently offline  frank
Messages: 48
Registered: May 2013
Karma: 0
Member
Shaun Bebbington <shaun@square-circle.co.uk> wrote:
> For clarification, which is faster?
>
> 10 X=INT(64*RND(0)+1)
>
> or
>
> 10 X%=64*RND(0)+1
>

you can enclose the two statements one at a time inside a 200-300 counts
FOR-NEXT cycle and easily verify yourself which is faster.
Frank
Re: C64 BASIC - what's faster INT() or %? Plus benefits of DEF FN A() [message #366421 is a reply to message #366396] Mon, 16 April 2018 22:46 Go to previous messageGo to next message
Anonymous
Karma:
Originally posted by: nospam.John.H..Guillory

SB> For clarification, which is faster?
SB> 10 X=INT(64*RND(0)+1)
SB> or
SB> 10 X%=64*RND(0)+1

Just guessing, I'd say the second one, or 10 X%=64*RND(0)+1
because in the first, your converting 64*RNND(0)+1 to an integer, then
converting back to a single precision decimal point value.
Re: C64 BASIC - what's faster INT() or %? Plus benefits of DEF FN A() [message #366443 is a reply to message #366421] Wed, 18 April 2018 09:38 Go to previous messageGo to next message
Anonymous
Karma:
Originally posted by: Shaun Bebbington

Thanks for the hints all. I'll do some bench mark testing or whatever :-)

Regards,

Shaun.
C64 BASIC - what's faster INT() or %? Plus benefits of DEF FN A() [message #366449 is a reply to message #366443] Wed, 18 April 2018 02:35 Go to previous messageGo to next message
Anonymous
Karma:
Originally posted by: nospam.Janne.Johansson

On 2018-04-18 05:38, Shaun Bebbington : All wrote:
> Thanks for the hints all. I'll do some bench mark testing or whatever :-)

Please do. If benchmarking with a FOR loop over hundreds or thousands of
repetitions is too hard to figure it out, then the answer isn't really
important.
Re: C64 BASIC - what's faster INT() or %? Plus benefits of DEF FN A() [message #366453 is a reply to message #366449] Wed, 18 April 2018 13:37 Go to previous messageGo to next message
Anonymous
Karma:
Originally posted by: Shaun Bebbington

On Wednesday, 18 April 2018 16:41:28 UTC+1, Janne Johansson wrote:
> On 2018-04-18 05:38, Shaun Bebbington : All wrote:
>> Thanks for the hints all. I'll do some bench mark testing or whatever :-)
>
> Please do. If benchmarking with a FOR loop over hundreds or thousands of
> repetitions is too hard to figure it out, then the answer isn't really
> important.

Hey! Good hint.
Re: C64 BASIC - what's faster INT() or %? Plus benefits of DEF FN A() [message #366468 is a reply to message #366453] Thu, 19 April 2018 02:34 Go to previous messageGo to next message
Anonymous
Karma:
Originally posted by: Shaun Bebbington

After some experimenting it seems that I've been doing random numbers in CBM BASIC completely wrong all of these years. Worse still, all of the examples in the printed literature at the time had the wrong example as well. I assume this is because firstly the least efficient way is more human readable, and secondly it would allow the range of numbers to exceed a 16-bit signed integer range.

Here is the fastest way to make peusdo-random numbers happen inside the CBM BASIC interpreter, assuming you want a positive integer of between zero and R:

X%=RND(.)*R

So, if you require a number higher than +32767, this is the fastest way:

X=INT(RND(.)*R)

The ordering seems to be important for optimisation as well, as R*RND(.) is slower because the interpreter re-orders the calculation on the fly according to its precedence.

I was never made aware of any of these facts as a child BASIC programmer.

Regards,

Shaun.
C64 BASIC - what's faster INT() or %? Plus benefits of DEF FN A() [message #366469 is a reply to message #366453] Wed, 18 April 2018 18:08 Go to previous messageGo to next message
Anonymous
Karma:
Originally posted by: nospam.Janne.Johansson

Re: C64 BASIC - what's faster INT() or %? Plus benefits of DEF FN A() [message #366474 is a reply to message #366469] Thu, 19 April 2018 04:14 Go to previous messageGo to next message
Anonymous
Karma:
Originally posted by: Shaun Bebbington

On Thursday, 19 April 2018 08:14:28 UTC+1, Janne Johansson wrote:
> On 2018-04-18 09:37, Shaun Bebbington : Janne Johansson wrote:
>> On Wednesday, 18 April 2018 16:41:28 UTC+1, Janne Johansson  wrote:
>>> On 2018-04-18 05:38, Shaun Bebbington : All wrote:
>>>> Thanks for the hints all. I'll do some bench mark testing or
>>> whatever :-)
>>>
>>> Please do. If benchmarking with a FOR loop over hundreds or thousands of
>>> repetitions is too hard to figure it out, then the answer isn't really
>>> important.
>>
>> Hey! Good hint.
>
> I noticed (after sending of course) that it may have been perceivable as
> a harsh statement, hinting at lazyness or something, I meant more along
> the lines of "if you try 100 loops and can't make out the difference,
> then try 1000 loops then try 10k loops and so on, the difference might
> be so small that its not worth coding your BASIC programs with %
> sprinkled here and there for optimization reasons, but just go for
> normal ordinary readability and simplicity since performance is then
> bound by something else like algorithmic complexity and not the one-time
> conversions from ints to floats and back".
>
> If you really need a basic program to run faster, there are a lot of
> compilers that pre-calculate and pre-parse and then make some kind of
> machine language equivalent program out of it which you can run and
> which will be lots faster. If that isn't fast enough still, code
> important parts in ASM directly or at least code it up in CC65 using
> C for some middle ground between compiled BASIC and doing it all in
> ASM yourself.

Yes Blitz BASIC seems to do a reasonably good job at this. As for C, well I love C. My thinking is like this: optimize BASIC, then compile. That should get the best results.

Regards,

Shaun.
Re: C64 BASIC - what's faster INT() or %? Plus benefits of DEF FN A() [message #366476 is a reply to message #366421] Thu, 19 April 2018 07:21 Go to previous messageGo to next message
Anonymous
Karma:
Originally posted by: Holger

Am 17.04.2018 um 04:46 schrieb John H. Guillory:
> SB> For clarification, which is faster?
> SB> 10 X=INT(64*RND(0)+1)
> SB> or
> SB> 10 X%=64*RND(0)+1
>
> Just guessing, I'd say the second one, or 10 X%=64*RND(0)+1
> because in the first, your converting 64*RNND(0)+1 to an integer, then
> converting back to a single precision decimal point value.
>
Effectively, calculations always happen in floating point, and INT()
returns a FP value in the floating point accu. Conversion will take
place on assignment to the destination variable. The INT() operation is
similar to the truncation operation to fit the FP accu value into X%.
In fact, the major difference is that the first line has to copy 5 bytes
into a variable space and the second will copy 2 bytes.

There isn't even an advantage in space requirement for the variable;
both need 7 bytes, i.e. 2 bytes for the name (like A1 or B2% or C3$) and
5 for the value; an integer variable just wastes 3 bytes for the value.
It is different for integer vs. float arrays; in this case a % value is
indeed packed into 2 bytes.

-hl
Re: C64 BASIC - what's faster INT() or %? Plus benefits of DEF FN A() [message #366489 is a reply to message #366476] Thu, 19 April 2018 15:53 Go to previous messageGo to next message
Anonymous
Karma:
Originally posted by: Shaun Bebbington

On Thursday, 19 April 2018 12:21:32 UTC+1, Holger wrote:
> Am 17.04.2018 um 04:46 schrieb John H. Guillory:
>> SB> For clarification, which is faster?
>> SB> 10 X=INT(64*RND(0)+1)
>> SB> or
>> SB> 10 X%=64*RND(0)+1
>>
>> Just guessing, I'd say the second one, or 10 X%=64*RND(0)+1
>> because in the first, your converting 64*RNND(0)+1 to an integer, then
>> converting back to a single precision decimal point value.
>>
> Effectively, calculations always happen in floating point, and INT()
> returns a FP value in the floating point accu. Conversion will take
> place on assignment to the destination variable. The INT() operation is
> similar to the truncation operation to fit the FP accu value into X%.
> In fact, the major difference is that the first line has to copy 5 bytes
> into a variable space and the second will copy 2 bytes.
>
> There isn't even an advantage in space requirement for the variable;
> both need 7 bytes, i.e. 2 bytes for the name (like A1 or B2% or C3$) and
> 5 for the value; an integer variable just wastes 3 bytes for the value.
> It is different for integer vs. float arrays; in this case a % value is
> indeed packed into 2 bytes.
>
> -hl

Thanks for the information, but using X% still saves two bytes and some processor time if used correctly. I'm looking for optimised BASIC as in speed where possible, rather than technically perfect BASIC which could never be achieved anyway.

I get enough performance from Blitz to be happy :-)

Regards,

Shaun.
Re: C64 BASIC - what's faster INT() or %? Plus benefits of DEF FN A() [message #366939 is a reply to message #366351] Fri, 27 April 2018 15:55 Go to previous messageGo to next message
Anonymous
Karma:
Originally posted by: Herr Doktor

What is the range of the random number you wish to generate? Have you
considered just reading the value of the system timer?

On Mon, 16 Apr 2018 09:20:04 -0700 (PDT), Shaun Bebbington
<shaun@square-circle.co.uk> wrote:

> There seems to be two ways to do integers on the C64 (and yes I know floats are fastest), I was wondering about these two methods of generating a random positive number between 1 and 64 inclusive:
>
> 10 X=INT(64*RND(0)+1)
>
> or
>
> 10 X%=64*RND(0)+1
>
> The latter saves two whole bytes, but is it faster to convert to integer directly with the INT keyword, or is the implicit declaration below faster?
>
> Second question, let's say you want to generate a random number in multiple places, you could use the above several times in a listing, but one could also define a function as I understand it, like:
>
> 0 DEF FN A(X)=X*RND(0)
>
> So each time in your listing, you will have,
>
> 10 X%=FN A(39): Y%=FN A(24)
> ...
> 100 S%=FN A(10)+1: D%=FN A(10)+1
>
> etc...
>
> Other than less typing throughout the program (where you might have lots of repeated random number generation in this example), are there any benefits to using CBM functions? Does it save bytes overall would be perhaps one that I could think of.
>
> Many thanks,
>
> Shaun.
Re: C64 BASIC - what's faster INT() or %? Plus benefits of DEF FN A() [message #366940 is a reply to message #366469] Fri, 27 April 2018 15:57 Go to previous messageGo to next message
Anonymous
Karma:
Originally posted by: Herr Doktor

I've used all the compilers, and prefer Petspeed.

On Thu, 19 Apr 2018 10:08:48 +1200,
nospam.Janne.Johansson@f6.n221.z2.binkp.net (Janne Johansson) wrote:

> On 2018-04-18 09:37, Shaun Bebbington : Janne Johansson wrote:
>> On Wednesday, 18 April 2018 16:41:28 UTC+1, Janne Johansson  wrote:
>>> On 2018-04-18 05:38, Shaun Bebbington : All wrote:
>>>> Thanks for the hints all. I'll do some bench mark testing or
>>> whatever :-)
>>>
>>> Please do. If benchmarking with a FOR loop over hundreds or thousands of
>>> repetitions is too hard to figure it out, then the answer isn't really
>>> important.
>>
>> Hey! Good hint.
>
> I noticed (after sending of course) that it may have been perceivable as
> a harsh statement, hinting at lazyness or something, I meant more along
> the lines of "if you try 100 loops and can't make out the difference,
> then try 1000 loops then try 10k loops and so on, the difference might
> be so small that its not worth coding your BASIC programs with %
> sprinkled here and there for optimization reasons, but just go for
> normal ordinary readability and simplicity since performance is then
> bound by something else like algorithmic complexity and not the one-time
> conversions from ints to floats and back".
>
> If you really need a basic program to run faster, there are a lot of
> compilers that pre-calculate and pre-parse and then make some kind of
> machine language equivalent program out of it which you can run and
> which will be lots faster. If that isn't fast enough still, code
> important parts in ASM directly or at least code it up in CC65 using
> C for some middle ground between compiled BASIC and doing it all in
> ASM yourself.
Re: C64 BASIC - what's faster INT() or %? Plus benefits of DEF FN A() [message #367122 is a reply to message #366940] Mon, 30 April 2018 04:36 Go to previous messageGo to next message
Anonymous
Karma:
Originally posted by: Shaun Bebbington

On Friday, 27 April 2018 20:57:21 UTC+1, Herr Doktor wrote:
> I've used all the compilers, and prefer Petspeed.
>
I couldn't get PETSpeed to work - maybe it doesn't like the way that I obfuscate and minimise my symbolic listings or something?

Regards,

Shaun
Re: C64 BASIC - what's faster INT() or %? Plus benefits of DEF FN A() [message #367357 is a reply to message #366476] Mon, 07 May 2018 15:06 Go to previous message
Charles Richmond is currently offline  Charles Richmond
Messages: 2676
Registered: December 2011
Karma: 0
Senior Member
On 4/19/2018 6:21 AM, Holger wrote:
> Am 17.04.2018 um 04:46 schrieb John H. Guillory:
>>   SB> For clarification, which is faster?
>>   SB> 10 X=INT(64*RND(0)+1)
>>   SB> or
>>   SB> 10 X%=64*RND(0)+1
>>
>> Just guessing, I'd say the second one, or 10 X%=64*RND(0)+1
>> because in the first, your converting 64*RNND(0)+1 to an integer, then
>> converting back to a single precision decimal point value.
>>
> Effectively, calculations always happen in floating point, and INT()
> returns a FP value in the floating point accu. Conversion will take
> place on assignment to the destination variable. The INT() operation is
> similar to the truncation operation to fit the FP accu value into X%.
> In fact, the major difference is that the first line has to copy 5 bytes
> into a variable space and the second will copy 2 bytes.
>
> There isn't even an advantage in space requirement for the variable;
> both need 7 bytes, i.e. 2 bytes for the name (like A1 or B2% or C3$) and
> 5 for the value; an integer variable just wastes 3 bytes for the value.
> It is different for integer vs. float arrays; in this case a % value is
> indeed packed into 2 bytes.
>

I am *not* sue about CBM, but C=64 BASIC does all its arithmetic in
floating point. That means if you have a line like:

120 X% = A% * B%

....this forces the interpreter to *convert* A% and B% to floating point,
then do the multiplication using the floating point routines, then
convert the answer back to an integer and store the answer in X%. Not
very efficient to do...


--
numerist at aquaporin4 dot com
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Maker Faire Bay Area - May 18-20, 2018
Next Topic: PET Snake 64 released
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Tue Jun 28 04:23:26 EDT 2022

Total time taken to generate the page: 0.00816 seconds