[Jprogramming] Computer Language Benchmarks Game

Alan K. Stebbens aks at stebbens.org
Fri Nov 30 19:05:06 HKT 2007


There is an interesting benchmarks "contest" at

   http://shootout.alioth.debian.org/

which includes a broad set of benchmark performance comparisons  
applied across a variety of programming languages.

As a stress relief, I've cobbled up a tentative submission for the  
"partial-sums" benchmark for the J language -- which currently has no  
coverage at all, surprisingly.

Here is the link to the partial-sums benchmark comparison:

   http://shootout.alioth.debian.org/gp4/benchmark.php? 
test=partialsums&lang=all

I've included my tentative submission below.

Before submitting this script to the contest, I would like to get it  
reviewed and possibly improved (it seems to already outperform all  
the other systems in the benchmark -- but I've not done a thorough  
evaluation either).

I would also invite others to contribute J solutions to the other  
benchmarks -- which would be an excellent way to see how J solutions  
can be applied to common programming problems.

If you are interested, this FAQ is worth reading:

   http://shootout.alioth.debian.org/gp4/faq.php


Thanks
--
Alan

-----

NB. partial-sums
NB.
NB. The Computer Language Benchmarks Game
NB.   http://shootout.alioth.debian.org/
NB.
NB.   contributed by Alan K. Stebbens <aks at stebbens.org>
NB.   modified by
NB.
NB.
NB. This script implements the partial-sums benchmark,
NB. as defined at:
NB.  http://shootout.alioth.debian.org/gp4/benchmark.php? 
test=partialsums&lang=all
NB.
NB.
NB. The "correct" output file for this benchmark test is defined
NB. at http://shootout.alioth.debian.org/gp4/iofile.php? 
test=partialsums&lang=all&file=output
NB.
NB. I've included the correct output here, for easy reference:
NB.
NB. 3.000000000		(2/3)^k
NB. 314.770573775	k^-0.5
NB. 0.999960002		1/k(k+1)
NB. 30.314520404	Flint Hills 		1/( k^3 * sin(k)^2 )
NB. 42.994899946	Cookson Hills		1/( k^3 * cos(k)^2 )
NB. 10.703866769	Harmonic		1/k
NB. 1.644894068		Riemann Zeta		1/(k^2)
NB. 0.693127181		Alternating Harmonic	-1^(k+1) / k
NB. 0.785388163		Gregory			-1^(k+1) / ( 2k - 1 )

time =: 6!:2

k =: i. 25000			NB. default input (for testing)

sum =: 0j9&":@(+/)		NB. 9 digits precision sum

sum1 =: (2%3)&^			NB. (2/3)^k
sum2 =: (_0.5&(^~))@>:		NB. k^(-0.5)
sum3 =: (%@(] * >:))@>:		NB. 1/(k * (k + 1))

sin =: 1&o.
cos =: 2&o.

sum4 =: %@((]^&3)*(*:@sin))@>:	NB. 1/((k^3)*(sin(k)^2))
sum5 =: %@((]^&3)*(*:@cos))@>:	NB. 1/((k^3)*(cos(k)^2))
sum6 =: %@>:			NB. 1/k
sum7 =: %@*:@>:			NB. 1/(k^2)
sum8 =: ((_1&^@>:)%])@>:	NB. -1^(k+1) / k
sum9 =: ((_1&^@>:)%(_1&+ at +:))@>: NB. -1^(k+1) / (2k - 1)

out =: 1!:2

sum_for =: 3 : 0
if. y-:'' do. k=.i.25000
else. k=.i.y end.
s=.,:''
s=.s,(sum sum1 k),'	(2/3)^k'
s=.s,(sum sum2 k),'	k^-0.5'
s=.s,(sum sum3 k),'	1/k(k+1)'
s=.s,(sum sum4 k),'	Flint Hills'
s=.s,(sum sum5 k),'	Cookson Hills'
s=.s,(sum sum6 k),'	Harmonic'
s=.s,(sum sum7 k),'	Riemann Zeta'
s=.s,(sum sum8 k),'	Alternating Harmonic'
s=.s,(sum sum9 k),'	Gregory'
s			NB. Show results
)





More information about the Programming mailing list