On Friday, 10 February 2012, Casper Bang wrote:

> On Thursday, February 9, 2012 11:47:30 PM UTC+1, KWright wrote:
>>
>> Bringing this back to the real world, did someone say imperative?  Lets
>> try declarative for a change :)
>>
>> import math._
>> val phi = (1+sqrt(5))/2.0d
>> def fib(n: Int) = round(pow(phi,n)/sqrt(5))
>>
>> (0 to 10) map (fib)
>> // = Vector(0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55)
>>
>
> That also looks like a completely different algorithm.
>
>

That was (at least partly) my point!  The Java/Scala solutions that have
been discussed so far are *also* very different algorithms.

It's disingenuous to compare two different solutions in two different
languages and then attribute any speed difference to the language used.
 Comparing apples and oranges doesn't really help anyone.

Yes, the amenability of functions as a first-class concept in Scala can
expose you to the cost of boxing, and this should be avoided in tight loops
and performance sensitive code.  But used sensibly it can be very powerful
in places where there *isn't* a performance bottleneck, such as the map
operation in the last line of my example.



> I would have used BigDecimal for accuracy, but that would need me to use
>> JScience or equivalent or roll my own pow, sqrt, and round functions - a
>> bit overkill for such a simple example!
>>
>> Rest assured that +, /, etc. work just fine for BigDecimals in Scala, and
>> therein lies the real power of the language; the stuff that should be
>> simple remains so, unhindered by boilerplate.
>>
>
> Java is horribly stuck in legacy, it should've had a decimal (base 10)
> type ages ago, but I digress. What I don't understand is, if Scala suffers
> from runtime penalties approaching an order of magnitude for this simple
> problem, under which conditions does it start to pay off
> automagically? Perhaps the culprit here is that Fibonacci is inherently a
> linear/serial problem, so prime number factorization might be a better
> example?!
>

Indeed!  That would be a good candidate for a tail-recursive solution :)


> /Casper
>
>
>

-- 
Kevin Wright
mail: [email protected]
gtalk / msn : [email protected]
quora: http://www.quora.com/Kevin-Wright
google+: http://gplus.to/thecoda
<[email protected]>
twitter: @thecoda
vibe / skype: kev.lee.wright
steam: kev_lee_wright

"My point today is that, if we wish to count lines of code, we should not
regard them as "lines produced" but as "lines spent": the current
conventional wisdom is so foolish as to book that count on the wrong side
of the ledger" ~ Dijkstra

-- 
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en.

Reply via email to