6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri Nov 22, 2024 6:16 pm

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Sat Jul 22, 2017 4:38 pm 
Offline
User avatar

Joined: Thu Mar 11, 2004 7:42 am
Posts: 362
As many of you know, Apple Pi is an Apple ("Integer") BASIC program that calculates pi to (up to) 1000 digits (using Machin's formula pi/4 = 4*arctan(1/5) - arctan(1/239)), was written by the late Bob Bishop, and appeared in the Aug/Sep 1978 issue of Micro, available here at 6502.org:

http://www.6502.org/documents/publicati ... g_1978.pdf

Apple Pi wasn't particularly fast (it took more than 40 hours to calculate pi to 1000 digits) and not much optimization was attempted. (For example, in Integer BASIC shorter variable names are faster, but rather than use one character variable names, longer, more descriptive names were used.)

In 1982 the late Glen Bredon (author of the Merlin assembler) ported Apple Pi to assembly. This port came with Merlin as an example program for the Merlin linker. (It was split into 5 source code files, plus 1 additional file of macro definitions.)

There were differences between the original BASIC program and the assembly port, including some optimizations. Some differences were minor; for example, the assembly port formatted digits neatly into columns, but the BASIC program simply output digits. Other differences were significant; for example, the assembly port divided by 57127 (=239*239) using 16-bit unsigned arithmetic, rather than dividing by 239 twice (as the BASIC program did, since Integer BASIC uses signed arithmetic and 239*239 does not fit into a signed 16 bit integer). Another difference was that the BASIC program uses two (decimal) digits per byte when calculating more than 200 digits, but the assembly program always uses 1 digit per byte. One added feature that the assembly port had was to find the first nonzero digit when summing the series. (Since the terms get smaller and smaller, they have more and more leading zeros as the calculation proceeds.)

So with the caveat that this is not a direct comparison between interpreted BASIC and assembly, but instead between BASIC and optimized assembly, how much faster is the assembly implementation than the BASIC implementation? Here are the results I measured on an Apple II (at ~1.02 MHz):

Code:
assembly:

 100 digits:   3 seconds
 200 digits:   9 seconds
 250 digits:  14 seconds
 400 digits:  33 seconds
1000 digits: 194 seconds = 3:14

BASIC:

 10 digits:   16 seconds
100 digits:  788 seconds =   13:08
250 digits: 9266 seconds = 2:34:26


(I wasn't able to summon the enthusiasm for waiting out a 40+ hour calculation in BASIC.)


Top
 Profile  
Reply with quote  
PostPosted: Sat Jul 22, 2017 5:08 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10985
Location: England
Good to see you here again - I might have guessed you'd show up on a day like today!


Top
 Profile  
Reply with quote  
PostPosted: Sat Jul 22, 2017 9:10 pm 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8505
Location: Midwestern USA
dclxvi wrote:
As many of you know, Apple Pi is an Apple ("Integer") BASIC program that calculates pi to (up to) 1000 digits...Apple Pi wasn't particularly fast (it took more than 40 hours to calculate pi to 1000 digits)...(I wasn't able to summon the enthusiasm for waiting out a 40+ hour calculation in BASIC.)

You've mildly aroused my curiosity. I may try running the BASIC program on my software development box, which is a dual processor 64 bit Linux machine on which Thoroughbred Dictionary-IV (a powerful, timesharing BASIC environment) is installed. It would be interesting to see how quickly a 1000 digit solution would be computed.

:cry: So many projects and so little time. :cry:

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
PostPosted: Sat Jul 22, 2017 10:21 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10985
Location: England
(For those interested in calculations of pi, see this thread for a newer and more efficient algorithm, implemented on a great variety of CPUs. But that doesn't help us compare Basic with machine code. Interesting that the performance ratio shown in the head post is so very different for 100 and for 250 digits.)


Top
 Profile  
Reply with quote  
PostPosted: Sun Jul 23, 2017 12:05 am 
Offline

Joined: Tue Jul 24, 2012 2:27 am
Posts: 679
BigEd wrote:
Interesting that the performance ratio shown in the head post is so very different for 100 and for 250 digits.

Pretty sure it has to do with this part:
Quote:
One added feature that the assembly port had was to find the first nonzero digit when summing the series. (Since the terms get smaller and smaller, they have more and more leading zeros as the calculation proceeds.)

Sounds like the later digits get more expensive to calculate, since the intermediate values have way more decimal digits to sum up.

_________________
WFDis Interactive 6502 Disassembler
AcheronVM: A Reconfigurable 16-bit Virtual CPU for the 6502 Microprocessor


Top
 Profile  
Reply with quote  
PostPosted: Sun Jul 23, 2017 7:21 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10985
Location: England
aha! That will be it. Thanks for the explanation.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 21 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: