The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Pattern Matching In Scala

1 reply on 1 page. Most recent reply: Mar 29, 2008 3:54 AM by Alistair McLean

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Alistair McLean

Posts: 2
Nickname: 54516
Registered: Mar, 2008

Pattern Matching In Scala Posted: Mar 29, 2008 2:59 AM
Reply to this message Reply
Advertisement
Hello all,

I've recently discovered Scala and i'm currently making my way through the excellent Programming in Scala e-book.

One of the things i do whenever i encounter a new language is to write the naive fibonacci algorithm as a quick test. So here was my first attempt :


object fibTest {

def fib (x: BigInt): BigInt = {
if (x == 0 || x == 1) x else fib (x - 1) + fib (x - 2);
}

def main (args: Array[String]) {
var count = 0;

while (count < 37) {
println (count + " => " + fib (count));
count += 1;
}
}
}


Running a time command on my macbook pro (core 2 duo, 2.4 ghz) i get the following results :


real 0m36.345s
user 0m35.354s
sys 0m0.371s


Not bad, faster than python, comparable to java, but nowhere near the C equivalent. Through reading the book i came across the concept of Pattern matching, so i thought i would re-write my naive fib to take advantage of it :


object pat {

def fibMatch (n: Int): Int = n match {
case 0 => n
case 1 => n
case _ => (fibMatch (n - 1) + fibMatch (n - 2))
}

def main (args: Array[String]) {
var count = 0;

while (count < 37) {
println (count + " => " + fibMatch (count));
count += 1;
}
}
}


I was surprised when i timed this :


real 0m1.325s
user 0m1.205s
sys 0m0.068s


So my question is, how can these differences be explained? Its the same naive algorithm as far as i can tell, no previous results are being held in arrays or matrices or anything. Obviously some sort of optimisation is done by the compiler in the pattern matching case, what is it? For the record when i use JIT compiling using the psyco module with python i get similar results :


real 0m2.180s
user 0m2.157s
sys 0m0.021s


Anyway, im back to read more of the book and see what else is in store!

Many thanks,

Alistair.


Alistair McLean

Posts: 2
Nickname: 54516
Registered: Mar, 2008

Re: Pattern Matching In Scala Posted: Mar 29, 2008 3:54 AM
Reply to this message Reply
Right i've figured it out, i used BigInt in the first example, and Int in the second, guess thats the performance difference for using BigInts. I feel pretty dumb now ;)

Flat View: This topic has 1 reply on 1 page
Topic: immutable HashMap Previous Topic   Next Topic Topic: Where are the Scala conferences/community?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use