On Sat, 19 Sep 2009, Jon Kleiser wrote:
Hi,
I wanted to compare how to do "sum of digits" in Scala and Pico Lisp, and
to my surprise my Pico Lisp version was much slower than my Scala version.
Well, Scala is a compiled language, but I hadn't anticipated that big a
difference. My test input number was a rather big one: (2^100 + 3^200)^300
(≈1.885 x 10^28627)
My Scala code was like this:
def sumOfDigits(n: BigInt): BigInt = n.toString.map{ _.asDigit
}.foldLeft(0){ _+_ }
val big = (2:BigInt).pow(100)+(3:BigInt).pow(200)
sumOfDigits(big.pow(300))
-> 128458
My Pico Lisp code was like this:
(de sumOfDigits (N)
(apply + (mapcar format (chop (format N)))) )
(setq Big (+ (** 2 100) (** 3 200)))
(sumOfDigits (** Big 300))
-> 128458
Could I have written the Pico Lisp code differently to make it faster?
1. avoid being redundant in the code, but it's not something that will make any
difference in speed here:
(de sumOfDigits (N)
(apply + (mapcar format (chop N))) )
# 'chop implicitly uses 'format, so there's no real reason to keep it there.
2. What's taking a long time here is not the 'sumOfDigits, but rather the
bigass number calculation and handling.
The bignum implementation was not intended to be.. *fast*, in the first
place. The implementation works with linked lists instead of arrays.
(setq Big (+ (** 2 100) (** 3 200))
Groso (** Big 300)
Venoso (format Big))
(sumOfDigits Venoso) # This alone should be *really* fast
So, what you'd really wanted to say in the original email is "Hey, scala has better bignum
implementation than pL" rather than "Hey, scala does 'sumOfDigits faster than pL"
(which is true, but derived from the first) ;-)
Solutions:
1. Use a C lib (like GMP) to do all the dirty work and then bring back the
result to pL to do the chopping and stuff.
2. To write a better bignum implementation for pL.
3. Cope with it(?)
--
UNSUBSCRIBE: mailto:picol...@software-lab.de?subject=unsubscribe