SlideShare a Scribd company logo
1 of 40
Performance Comparisons of Dynamic Languages
on the Java Virtual Machine
Michael Galpin, eBay
@michaelg
The Critical Factor When Choosing JVM Language?
SPEED
The End
Not Just Speed.
Stability
               ne ss
        sive
    res                       Man
E xp                              agea
                                           bility
                Not Just Speed.




                                    Comm
       To
        ol




                         y
                       Jo
          s




                                     unity
Disclaimer
Disclaimer




I’m a Liar
Three Algorithms & Six Languages

• Prime Number Sieve           • Groovy


• Case Insensitive Word Sort   • JRuby


• Reversible Numbers           • Jython


                               • Clojure


                               • Scala


                               • Fan
Three Algorithms & Six Languages

• Prime Number Sieve           • Groovy


• Case Insensitive Word Sort   • JRuby


• Reversible Numbers           • Jython


                               • Clojure


                               • Scala


                               • Fan
Prime Number Sieve
public class Primes {
    final private List<Integer> primes;
    private final int size;

    public Primes(int n){
        size = n;
        primes = new ArrayList<Integer>(n);
        init();
    }

    private void init(){
        int i=2;
        int max = calcSize();
        List<Boolean> nums = new ArrayList<Boolean>(max);
        for (int j=0;j<max;j++){
            nums.add(true);
        }
        while (i < max && primes.size() < size){
            int p = i;
            if (nums.get(i)){
                primes.add(p);
                 int j = 2*p;
                 while (j < max - p){
                     nums.set(j,false);
                     j += p;
                }
            }
            i += 1;
        }
    }

    public int last(){
        return primes.get(primes.size()-1);
    }

    private int calcSize(){
        int max = 2;
        while ((max/Math.log(max)) < size && max < Integer.MAX_VALUE && max > 0){
            max *=2;
        }
        return max;
    }
}
class RubyPrimes
  attr_accessor :cnt, :primes

  def initialize(n)
    @primes = Array.new(n,0)
    @cnt = 0
    i = 2
    max = calcSize
    nums = Array.new(max,true)
    while (i < max && @cnt < @primes.length)
      p = i
      if (nums[i])
        @primes[@cnt] = p
        @cnt += 1
        (p .. (max/p)).each{ |j| nums[p*j] = false }
      end
      i += 1
    end
  end

  def calcSize
    max = 2
    max = max * 2 while ( max/Math.log(max) < @primes.length)
    max
  end

  def last
    @primes[@cnt -1]
  end
end
class PyPrimes(object):
    __slots__ = ['cnt','primes','size']

    def __init__(self,n):
        self.primes = n*[0]
        self.cnt = 0
        self.size = n
        i = 2
        max = self.calcSize()
        nums = max*[True]
        while i < max and self.cnt < self.size:
            p = i
            if nums[i]:
                self.primes[self.cnt] = p
                self.cnt += 1
                for j in xrange(p, max/p):
                    nums[p*j] = False
            i+= 1

    def calcSize(self):
        max = 2
        while max/log(max) < self.size:
            max = max *2 # this makes the sieve too big, but fast
        return max

    def last(self):
        return self.primes[self.cnt - 1]
Time / Java Time
class ScalaPrimes(val size:Int){
  val primes = new ArrayBuffer[Int]
  var cnt = 0
  init
  def last = primes(primes.size - 1)

    private
    def init {
      var i = 2
      val max = calcSize
      val nums = new ArrayBuffer[Boolean]
      for (i<-0 until max) nums += true
      while (i < max && cnt < size){
        val p = i
        if (nums(i)){
          primes += p
          cnt += 1
          (p until max/p).foreach((j) => nums.update(p*j,false))
        }
        i += 1
      }
    }
    def calcSize = {
      var max = 2
      while ( (max/log(max)) < size && max < MAX_VALUE && max > 0) {
        max *= 2
      }
      max
    }
}
class Primes {
  Int[] primes := Int[,]
  Int size

    new make(Int n) {
      size = n
      primes.capacity = n
      init
    }

    private Void init() {
      i := 2
      max := calcSize
      nums := Bool[,]
      nums.fill(true, max)
      while (i < max && primes.size < size) {
        p := i
        if (nums[i])
        {
          primes.add(p)
          j := 2*p;
          while (j < max - p) {
            nums[j] = false
            j += p;
          }
        }
        i += 1;
      }
    }

        Int last() { primes.last }

        private Int calcSize() {
          max := 2
          while ((max.toFloat/max.toFloat.log).toInt < size && max < 0x7fff_ffff && max > 0) {
             max *=2; // memory inefficient, but fast
          }
          return max;
    }
}
Time / Java Time
Word Sort
public class WordSort {
    private final File dataFile;
    private final List<String> words;

    public WordSort(String fileName){
        dataFile = new File(fileName);
        int idx = fileName.lastIndexOf(quot;/quot;);
        String name = fileName.substring(idx+1, fileName.length() - 4);
        int numWords = Integer.parseInt(name);
        words = new ArrayList<String>(numWords);
    }

    public List<String> getSortedWords(){
        if (words.size() > 0){
            return words;
        }
        try {
            BufferedReader reader = new BufferedReader(new FileReader(dataFile));
            try{
                 String line;
                 while ( (line = reader.readLine()) != null){
                     for (String string : line.split(quot; quot;)){
                         words.add(string);
                     }
                }
            } finally {
                reader.close();
            }
            Collections.sort(words, new Comparator<String>(){
                 public int compare(String s, String s1) {
                     return s.toLowerCase().compareTo(s1.toLowerCase());
                }
            });
            return words;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
public class GroovyWordSort {
  File dataFile
  List words = []

    public GroovyWordSort(fileName){
      dataFile = new File(fileName)
    }

    def sortedWords(){
      if (!words){
        dataFile.eachLine{it.tokenize().each {words.add(it) }}
        words = words.sort{w,w1 -> w.toLowerCase() <=> w1.toLowerCase()}
      }
      words
    }
}
class RubyWordSort

  def initialize(fileName)
    @dataFile = File.new(fileName)
    @words = Array.new
  end

  def sortedWorts
    if (@words.length == 0)
      @dataFile.each_line{ |line| line.split(' ').each{ |word| @words << word}}
      @words = @words.sort{ |w,w1| w.upcase <=> w1.upcase }
    end
    @words
  end

end
class PyWordSort(object):

    def __init__(self, fileName):
        print quot;init quot; + fileName
        self.dataFile = open(fileName)
        self.words = []

    def sortedWords(self):
        if len(self.words) == 0:
            for line in self.dataFile:
                for word in line.split():
                    self.words.append(word)
            self.words.sort(lambda w,w1: cmp(w.lower(), w1.lower()))
        return self.words
(defn word-sort [file-name]
    (with-open [r (new java.io.BufferedReader (new java.io.FileReader
    file-name))]
      (sort-by #(.toLowerCase #^String %)
               (mapcat #(.split #^String % quot; quot;) (line-seq r)))))
class ScalaWordSort(fileName:String){
  lazy val sortedWords = {
    Source.fromFile(dataFile).getLines.foreach(_.split(quot; quot;).foreach(words ::= _))
    words.sort(_.toLowerCase < _.toLowerCase)
  }
  private
  val dataFile = new File(fileName)
  var words:List[String] = Nil
}
class WordSort {
  File? dataFile

    once Str[] getSortedWords() {
      words := Str[,]
      dataFile.eachLine |line| { words.addAll(line.split) }
      return words.sort |a, b| { a.compareIgnoreCase(b) }

    }
}
Time / Java Time
Reversible Numbers
WTF is a Reversible Number?



Some positive integers n have the property that the sum [ n + reverse(n) ] consists entirely of odd
(decimal) digits. For instance, 36 + 63 = 99 and 409 + 904 = 1313. We will call such numbers
reversible; so 36, 63, 409, and 904 are reversible. Leading zeroes are not allowed in either n or
reverse(n).

There are 120 reversible numbers below one-thousand.
public class Reversible {
    final int max;
    final int numReversible;

    public Reversible(int max){
        this.max = max;
        this.numReversible = this.countReversible();
    }

    public int countReversible(){
        if (numReversible > 0){
            return numReversible;
        }
        int cnt = 0;
        for (int i=11;i<=max;i++){
            if (reversible(i)) {
                cnt++;
            }
        }
        return cnt;
    }

    private boolean reversible(int n){
        return allOdd(reverse(n));
    }

    private boolean allOdd(int n){
        String str = Integer.toString(n);
        for (int i=0;i<str.length();i++){
            int m = Character.digit(str.charAt(i),10);
            if (m % 2 == 0) return false;
        }
        return true;
    }

    private int reverse(Integer n){
        char[] digits = n.toString().toCharArray();
        char[] rev = new char[digits.length];
        for (int i=digits.length-1;i>=0;i--){
            rev[i] = digits[digits.length -i-1];
        }
        return n + Integer.parseInt(String.valueOf(rev));
    }
}
public class GroovyReversible {
  final Integer max
  final Integer numReversible = 0

    public GroovyReversible(max){
      this.max = max
      this.numReversible = this.countReversible()
    }

    def countReversible(){
      numReversible ?: (11..max).findAll{reversible(it)}.size()
    }

    def reversible(n){
      allOdd(reverse(n))
    }

    def allOdd(n){
      n.toString().toList().collect {it.toInteger()}.every{it % 2 == 1}
    }

    def reverse(n){
      n + n.toString().toList().reverse().join().toInteger()
    }
}
class RubyReversible
  def initialize(max)
    @max = max
    @numReversible = nil
  end

  def allOdd(n)
    digits = n.to_s.split(//)
    digits.length == digits.map{|c| c.to_i}.select{|i| i % 2 == 1}.length
  end

  def reverse(n)
    n + n.to_s.reverse.to_i
  end

  def reversible(n)
    allOdd(reverse(n))
  end

  def countReversible()
    @numReversible ||= (11..@max).select{|i| reversible(i)}.length
    @numReversible
  end
end
class PyReversible(object):
    __slots__ = ['max','numReversible']

    def __init__(self, max):
        self.max = max
        self.numReversible = self.countReversible()

    def countReversible(self):
        return len([i for i in xrange(11,self.max+1) if self.reversible(i)])

    def allOdd(self,n):
        for ch in str(n):
            if int(ch) % 2 != 1:
                return False
        return True

    def reverse(self,n):
        return n + int(str(n)[::-1])

    def reversible(self,n):
        return self.allOdd(self.reverse(n))
(defn all-odd? [n]
  (every? odd? (map #(Integer. (str %)) (str n))))

(defn reverse-num [n]
  (+ n (Integer. (apply str (reverse (str n))))))

(defn reversible-num? [n]
  (all-odd? (reverse-num n)))

(defn count-reversible [rev-max]
  {:max rev-max
   :num-reversible (count (filter reversible-num? (range 11 rev-max)))})
class ScalaReversible(max:Int){
  lazy val numReversible = (11 to max).filter(reversible(_)).size
  private
  def reverse(n:Int)=n + parseInt(n.toString.reverse)
  def allOdd(n:Int) = n.toString.map(digit(_,10)).forall(_ % 2 == 1)
  def reversible(n:Int) = allOdd(reverse(n))
class Reversible {
  Int max

    once Int   countReversible() {
      cnt :=   0
      for (i   := 11; i<=max; i++) if (reversible(i)) cnt++
      return   cnt
    }

    private Bool reversible(Int n) {
      allOdd(reverse(n))
    }

    private Bool allOdd(Int n) {
      n.toStr.all |char| { char.isOdd }
    }

    private Int reverse(Int n) {
      n + n.toStr.reverse.toInt

    }
}
Time / Java Time
So What Now?
Questions?

More Related Content

Viewers also liked

Why Perl, when you can use bash+awk+sed? :P
Why Perl, when you can use bash+awk+sed? :PWhy Perl, when you can use bash+awk+sed? :P
Why Perl, when you can use bash+awk+sed? :P
Luciano Rocha
 
Kulturkartläggnings processen
Kulturkartläggnings  processenKulturkartläggnings  processen
Kulturkartläggnings processen
Lina Ydrefelt
 
Open Education and Sustainability
Open Education and SustainabilityOpen Education and Sustainability
Open Education and Sustainability
Joss Winn
 
Collaborative Assessment
Collaborative AssessmentCollaborative Assessment
Collaborative Assessment
Jennifer Orr
 
新聞整理五則
新聞整理五則新聞整理五則
新聞整理五則
guest370229
 
Antelope by Kijana
Antelope by KijanaAntelope by Kijana
Antelope by Kijana
vebrya
 

Viewers also liked (20)

How Recommender Systems in Technology-Enhanced Learning depend on Context
How Recommender Systems in Technology-Enhanced Learning depend on ContextHow Recommender Systems in Technology-Enhanced Learning depend on Context
How Recommender Systems in Technology-Enhanced Learning depend on Context
 
A Long Walk to Water: Lesson5 unit3
A Long Walk to Water:  Lesson5 unit3A Long Walk to Water:  Lesson5 unit3
A Long Walk to Water: Lesson5 unit3
 
Project Bid
Project BidProject Bid
Project Bid
 
Escalate 020608
Escalate 020608Escalate 020608
Escalate 020608
 
Dales Garden 5 10 08 Presentation4
Dales Garden 5 10 08 Presentation4Dales Garden 5 10 08 Presentation4
Dales Garden 5 10 08 Presentation4
 
warming
warmingwarming
warming
 
Why Perl, when you can use bash+awk+sed? :P
Why Perl, when you can use bash+awk+sed? :PWhy Perl, when you can use bash+awk+sed? :P
Why Perl, when you can use bash+awk+sed? :P
 
Work Contracts
Work ContractsWork Contracts
Work Contracts
 
Kulturkartläggnings processen
Kulturkartläggnings  processenKulturkartläggnings  processen
Kulturkartläggnings processen
 
Open Education and Sustainability
Open Education and SustainabilityOpen Education and Sustainability
Open Education and Sustainability
 
Collaborative Assessment
Collaborative AssessmentCollaborative Assessment
Collaborative Assessment
 
Lezione 2011
Lezione 2011Lezione 2011
Lezione 2011
 
Gençlik Danışmanlık ve Sağlık Hizmeti Merkezi
Gençlik Danışmanlık ve Sağlık Hizmeti MerkeziGençlik Danışmanlık ve Sağlık Hizmeti Merkezi
Gençlik Danışmanlık ve Sağlık Hizmeti Merkezi
 
sd
sdsd
sd
 
新聞整理五則
新聞整理五則新聞整理五則
新聞整理五則
 
Famous slogans
Famous slogansFamous slogans
Famous slogans
 
Antelope by Kijana
Antelope by KijanaAntelope by Kijana
Antelope by Kijana
 
Athina
AthinaAthina
Athina
 
Phoenix Grid Alternatives Solar Rehabilitation Presentation
Phoenix   Grid Alternatives Solar Rehabilitation PresentationPhoenix   Grid Alternatives Solar Rehabilitation Presentation
Phoenix Grid Alternatives Solar Rehabilitation Presentation
 
生命を理解する道具としての計算機  SCSN@UCLA
生命を理解する道具としての計算機  SCSN@UCLA生命を理解する道具としての計算機  SCSN@UCLA
生命を理解する道具としての計算機  SCSN@UCLA
 

More from Michael Galpin

More from Michael Galpin (13)

Android lessons you won't learn in school
Android lessons you won't learn in schoolAndroid lessons you won't learn in school
Android lessons you won't learn in school
 
Design Patterns for Tablets and Smartphones
Design Patterns for Tablets and SmartphonesDesign Patterns for Tablets and Smartphones
Design Patterns for Tablets and Smartphones
 
Android workshop
Android workshopAndroid workshop
Android workshop
 
Scala on Android: Experiences at Bump Technologies
Scala on Android: Experiences at Bump TechnologiesScala on Android: Experiences at Bump Technologies
Scala on Android: Experiences at Bump Technologies
 
That’s My App - Running in Your Background - Draining Your Battery
That’s My App - Running in Your Background - Draining Your BatteryThat’s My App - Running in Your Background - Draining Your Battery
That’s My App - Running in Your Background - Draining Your Battery
 
Mobile Web 5.0
Mobile Web 5.0Mobile Web 5.0
Mobile Web 5.0
 
Persistent Data Structures And Managed References
Persistent Data Structures And Managed ReferencesPersistent Data Structures And Managed References
Persistent Data Structures And Managed References
 
Scala on Your Phone
Scala on Your PhoneScala on Your Phone
Scala on Your Phone
 
Mobile Development 101
Mobile Development 101Mobile Development 101
Mobile Development 101
 
RIAs Done Right: Grails, Flex, and EXT GWT
RIAs Done Right: Grails, Flex, and EXT GWTRIAs Done Right: Grails, Flex, and EXT GWT
RIAs Done Right: Grails, Flex, and EXT GWT
 
Eclipse @eBay 2009
Eclipse @eBay 2009Eclipse @eBay 2009
Eclipse @eBay 2009
 
Introduction to Scala for Java Developers
Introduction to Scala for Java DevelopersIntroduction to Scala for Java Developers
Introduction to Scala for Java Developers
 
Eclipse@eBay
Eclipse@eBayEclipse@eBay
Eclipse@eBay
 

Recently uploaded

ppt seni budaya kelas xi, menganalisis konsep,unsur,prinsip dan teknik
ppt seni budaya kelas xi, menganalisis konsep,unsur,prinsip dan teknikppt seni budaya kelas xi, menganalisis konsep,unsur,prinsip dan teknik
ppt seni budaya kelas xi, menganalisis konsep,unsur,prinsip dan teknik
Agustinus791932
 
Van Gogh Powerpoint for art lesson today
Van Gogh Powerpoint for art lesson todayVan Gogh Powerpoint for art lesson today
Van Gogh Powerpoint for art lesson today
lucygibson17
 
WhatsApp-(# 9711106444 #)Call Girl in Noida Sector 80 Noida (Escorts) Delhi
WhatsApp-(# 9711106444 #)Call Girl in Noida Sector 80 Noida (Escorts) DelhiWhatsApp-(# 9711106444 #)Call Girl in Noida Sector 80 Noida (Escorts) Delhi
WhatsApp-(# 9711106444 #)Call Girl in Noida Sector 80 Noida (Escorts) Delhi
delhimunirka15
 
Call Girls In Dilshad Garden | Contact Me ☎ +91-9953040155
Call Girls In Dilshad Garden | Contact Me ☎ +91-9953040155Call Girls In Dilshad Garden | Contact Me ☎ +91-9953040155
Call Girls In Dilshad Garden | Contact Me ☎ +91-9953040155
SaketCallGirlsCallUs
 
Azad Nagar Call Girls ,☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genuin...
Azad Nagar Call Girls ,☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genuin...Azad Nagar Call Girls ,☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genuin...
Azad Nagar Call Girls ,☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genuin...
delhimunirka15
 
Call Girls In Dwarka Mor | Contact Me ☎ +91-9953040155
Call Girls In Dwarka Mor | Contact Me ☎ +91-9953040155Call Girls In Dwarka Mor | Contact Me ☎ +91-9953040155
Call Girls In Dwarka Mor | Contact Me ☎ +91-9953040155
SaketCallGirlsCallUs
 
Nehru Nagar, Call Girls ☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genui...
Nehru Nagar, Call Girls ☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genui...Nehru Nagar, Call Girls ☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genui...
Nehru Nagar, Call Girls ☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genui...
delhimunirka15
 
Call Girls In Sindhudurg Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service E...
Call Girls In Sindhudurg Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service E...Call Girls In Sindhudurg Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service E...
Call Girls In Sindhudurg Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service E...
Nitya salvi
 
Museum of fine arts Lauren Simpson…………..
Museum of fine arts Lauren Simpson…………..Museum of fine arts Lauren Simpson…………..
Museum of fine arts Lauren Simpson…………..
mvxpw22gfc
 

Recently uploaded (20)

ppt seni budaya kelas xi, menganalisis konsep,unsur,prinsip dan teknik
ppt seni budaya kelas xi, menganalisis konsep,unsur,prinsip dan teknikppt seni budaya kelas xi, menganalisis konsep,unsur,prinsip dan teknik
ppt seni budaya kelas xi, menganalisis konsep,unsur,prinsip dan teknik
 
SB_ Pretzel and the puppies_ Rough_ RiverPhan (2024)
SB_ Pretzel and the puppies_ Rough_ RiverPhan (2024)SB_ Pretzel and the puppies_ Rough_ RiverPhan (2024)
SB_ Pretzel and the puppies_ Rough_ RiverPhan (2024)
 
Navsari Call Girl 📞 8617370543 Low Price Genuine Service
Navsari Call Girl 📞 8617370543 Low Price Genuine ServiceNavsari Call Girl 📞 8617370543 Low Price Genuine Service
Navsari Call Girl 📞 8617370543 Low Price Genuine Service
 
SB_ Scott Pilgrim_ Rough_ RiverPhan (2024)
SB_ Scott Pilgrim_ Rough_ RiverPhan (2024)SB_ Scott Pilgrim_ Rough_ RiverPhan (2024)
SB_ Scott Pilgrim_ Rough_ RiverPhan (2024)
 
Van Gogh Powerpoint for art lesson today
Van Gogh Powerpoint for art lesson todayVan Gogh Powerpoint for art lesson today
Van Gogh Powerpoint for art lesson today
 
Russian Call Girls In Bhubaneswar 📱 Odisha 9777949614 Indore
Russian Call Girls In Bhubaneswar 📱 Odisha 9777949614 IndoreRussian Call Girls In Bhubaneswar 📱 Odisha 9777949614 Indore
Russian Call Girls In Bhubaneswar 📱 Odisha 9777949614 Indore
 
WhatsApp-(# 9711106444 #)Call Girl in Noida Sector 80 Noida (Escorts) Delhi
WhatsApp-(# 9711106444 #)Call Girl in Noida Sector 80 Noida (Escorts) DelhiWhatsApp-(# 9711106444 #)Call Girl in Noida Sector 80 Noida (Escorts) Delhi
WhatsApp-(# 9711106444 #)Call Girl in Noida Sector 80 Noida (Escorts) Delhi
 
Orai call girls 📞 8617370543At Low Cost Cash Payment Booking
Orai call girls 📞 8617370543At Low Cost Cash Payment BookingOrai call girls 📞 8617370543At Low Cost Cash Payment Booking
Orai call girls 📞 8617370543At Low Cost Cash Payment Booking
 
Call Girls In Dilshad Garden | Contact Me ☎ +91-9953040155
Call Girls In Dilshad Garden | Contact Me ☎ +91-9953040155Call Girls In Dilshad Garden | Contact Me ☎ +91-9953040155
Call Girls In Dilshad Garden | Contact Me ☎ +91-9953040155
 
Azamgarh Call Girls WhatsApp Chat: 📞 8617370543 (24x7 ) Service Available Nea...
Azamgarh Call Girls WhatsApp Chat: 📞 8617370543 (24x7 ) Service Available Nea...Azamgarh Call Girls WhatsApp Chat: 📞 8617370543 (24x7 ) Service Available Nea...
Azamgarh Call Girls WhatsApp Chat: 📞 8617370543 (24x7 ) Service Available Nea...
 
WhatsApp Chat: 📞 8617370543 Call Girls In Siddharth Nagar At Low Cost Cash Pa...
WhatsApp Chat: 📞 8617370543 Call Girls In Siddharth Nagar At Low Cost Cash Pa...WhatsApp Chat: 📞 8617370543 Call Girls In Siddharth Nagar At Low Cost Cash Pa...
WhatsApp Chat: 📞 8617370543 Call Girls In Siddharth Nagar At Low Cost Cash Pa...
 
Azad Nagar Call Girls ,☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genuin...
Azad Nagar Call Girls ,☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genuin...Azad Nagar Call Girls ,☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genuin...
Azad Nagar Call Girls ,☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genuin...
 
Call Girls In Dwarka Mor | Contact Me ☎ +91-9953040155
Call Girls In Dwarka Mor | Contact Me ☎ +91-9953040155Call Girls In Dwarka Mor | Contact Me ☎ +91-9953040155
Call Girls In Dwarka Mor | Contact Me ☎ +91-9953040155
 
Nehru Nagar, Call Girls ☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genui...
Nehru Nagar, Call Girls ☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genui...Nehru Nagar, Call Girls ☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genui...
Nehru Nagar, Call Girls ☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genui...
 
Call Girls In Sindhudurg Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service E...
Call Girls In Sindhudurg Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service E...Call Girls In Sindhudurg Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service E...
Call Girls In Sindhudurg Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service E...
 
Turn Off The Air Con - The Singapore Punk Scene
Turn Off The Air Con - The Singapore Punk SceneTurn Off The Air Con - The Singapore Punk Scene
Turn Off The Air Con - The Singapore Punk Scene
 
Museum of fine arts Lauren Simpson…………..
Museum of fine arts Lauren Simpson…………..Museum of fine arts Lauren Simpson…………..
Museum of fine arts Lauren Simpson…………..
 
HUMA Final Presentation About Chicano Culture
HUMA Final Presentation About Chicano CultureHUMA Final Presentation About Chicano Culture
HUMA Final Presentation About Chicano Culture
 
Call Girls Sultanpur Just Call 📞 8617370543 Top Class Call Girl Service Avail...
Call Girls Sultanpur Just Call 📞 8617370543 Top Class Call Girl Service Avail...Call Girls Sultanpur Just Call 📞 8617370543 Top Class Call Girl Service Avail...
Call Girls Sultanpur Just Call 📞 8617370543 Top Class Call Girl Service Avail...
 
Call Girls Bhavnagar - 📞 8617370543 Our call girls are sure to provide you wi...
Call Girls Bhavnagar - 📞 8617370543 Our call girls are sure to provide you wi...Call Girls Bhavnagar - 📞 8617370543 Our call girls are sure to provide you wi...
Call Girls Bhavnagar - 📞 8617370543 Our call girls are sure to provide you wi...
 

Performance Comparisons of Dynamic Languages on the Java Virtual Machine

  • 1. Performance Comparisons of Dynamic Languages on the Java Virtual Machine Michael Galpin, eBay @michaelg
  • 2. The Critical Factor When Choosing JVM Language?
  • 4.
  • 7. Stability ne ss sive res Man E xp agea bility Not Just Speed. Comm To ol y Jo s unity
  • 10. Three Algorithms & Six Languages • Prime Number Sieve • Groovy • Case Insensitive Word Sort • JRuby • Reversible Numbers • Jython • Clojure • Scala • Fan
  • 11. Three Algorithms & Six Languages • Prime Number Sieve • Groovy • Case Insensitive Word Sort • JRuby • Reversible Numbers • Jython • Clojure • Scala • Fan
  • 13. public class Primes { final private List<Integer> primes; private final int size; public Primes(int n){ size = n; primes = new ArrayList<Integer>(n); init(); } private void init(){ int i=2; int max = calcSize(); List<Boolean> nums = new ArrayList<Boolean>(max); for (int j=0;j<max;j++){ nums.add(true); } while (i < max && primes.size() < size){ int p = i; if (nums.get(i)){ primes.add(p); int j = 2*p; while (j < max - p){ nums.set(j,false); j += p; } } i += 1; } } public int last(){ return primes.get(primes.size()-1); } private int calcSize(){ int max = 2; while ((max/Math.log(max)) < size && max < Integer.MAX_VALUE && max > 0){ max *=2; } return max; } }
  • 14. class RubyPrimes attr_accessor :cnt, :primes def initialize(n) @primes = Array.new(n,0) @cnt = 0 i = 2 max = calcSize nums = Array.new(max,true) while (i < max && @cnt < @primes.length) p = i if (nums[i]) @primes[@cnt] = p @cnt += 1 (p .. (max/p)).each{ |j| nums[p*j] = false } end i += 1 end end def calcSize max = 2 max = max * 2 while ( max/Math.log(max) < @primes.length) max end def last @primes[@cnt -1] end end
  • 15. class PyPrimes(object): __slots__ = ['cnt','primes','size'] def __init__(self,n): self.primes = n*[0] self.cnt = 0 self.size = n i = 2 max = self.calcSize() nums = max*[True] while i < max and self.cnt < self.size: p = i if nums[i]: self.primes[self.cnt] = p self.cnt += 1 for j in xrange(p, max/p): nums[p*j] = False i+= 1 def calcSize(self): max = 2 while max/log(max) < self.size: max = max *2 # this makes the sieve too big, but fast return max def last(self): return self.primes[self.cnt - 1]
  • 16. Time / Java Time
  • 17. class ScalaPrimes(val size:Int){ val primes = new ArrayBuffer[Int] var cnt = 0 init def last = primes(primes.size - 1) private def init { var i = 2 val max = calcSize val nums = new ArrayBuffer[Boolean] for (i<-0 until max) nums += true while (i < max && cnt < size){ val p = i if (nums(i)){ primes += p cnt += 1 (p until max/p).foreach((j) => nums.update(p*j,false)) } i += 1 } } def calcSize = { var max = 2 while ( (max/log(max)) < size && max < MAX_VALUE && max > 0) { max *= 2 } max } }
  • 18. class Primes { Int[] primes := Int[,] Int size new make(Int n) { size = n primes.capacity = n init } private Void init() { i := 2 max := calcSize nums := Bool[,] nums.fill(true, max) while (i < max && primes.size < size) { p := i if (nums[i]) { primes.add(p) j := 2*p; while (j < max - p) { nums[j] = false j += p; } } i += 1; } } Int last() { primes.last } private Int calcSize() { max := 2 while ((max.toFloat/max.toFloat.log).toInt < size && max < 0x7fff_ffff && max > 0) { max *=2; // memory inefficient, but fast } return max; } }
  • 19. Time / Java Time
  • 21. public class WordSort { private final File dataFile; private final List<String> words; public WordSort(String fileName){ dataFile = new File(fileName); int idx = fileName.lastIndexOf(quot;/quot;); String name = fileName.substring(idx+1, fileName.length() - 4); int numWords = Integer.parseInt(name); words = new ArrayList<String>(numWords); } public List<String> getSortedWords(){ if (words.size() > 0){ return words; } try { BufferedReader reader = new BufferedReader(new FileReader(dataFile)); try{ String line; while ( (line = reader.readLine()) != null){ for (String string : line.split(quot; quot;)){ words.add(string); } } } finally { reader.close(); } Collections.sort(words, new Comparator<String>(){ public int compare(String s, String s1) { return s.toLowerCase().compareTo(s1.toLowerCase()); } }); return words; } catch (IOException e) { throw new RuntimeException(e); } } }
  • 22. public class GroovyWordSort { File dataFile List words = [] public GroovyWordSort(fileName){ dataFile = new File(fileName) } def sortedWords(){ if (!words){ dataFile.eachLine{it.tokenize().each {words.add(it) }} words = words.sort{w,w1 -> w.toLowerCase() <=> w1.toLowerCase()} } words } }
  • 23. class RubyWordSort def initialize(fileName) @dataFile = File.new(fileName) @words = Array.new end def sortedWorts if (@words.length == 0) @dataFile.each_line{ |line| line.split(' ').each{ |word| @words << word}} @words = @words.sort{ |w,w1| w.upcase <=> w1.upcase } end @words end end
  • 24. class PyWordSort(object): def __init__(self, fileName): print quot;init quot; + fileName self.dataFile = open(fileName) self.words = [] def sortedWords(self): if len(self.words) == 0: for line in self.dataFile: for word in line.split(): self.words.append(word) self.words.sort(lambda w,w1: cmp(w.lower(), w1.lower())) return self.words
  • 25. (defn word-sort [file-name] (with-open [r (new java.io.BufferedReader (new java.io.FileReader file-name))] (sort-by #(.toLowerCase #^String %) (mapcat #(.split #^String % quot; quot;) (line-seq r)))))
  • 26. class ScalaWordSort(fileName:String){ lazy val sortedWords = { Source.fromFile(dataFile).getLines.foreach(_.split(quot; quot;).foreach(words ::= _)) words.sort(_.toLowerCase < _.toLowerCase) } private val dataFile = new File(fileName) var words:List[String] = Nil }
  • 27. class WordSort { File? dataFile once Str[] getSortedWords() { words := Str[,] dataFile.eachLine |line| { words.addAll(line.split) } return words.sort |a, b| { a.compareIgnoreCase(b) } } }
  • 28. Time / Java Time
  • 30. WTF is a Reversible Number? Some positive integers n have the property that the sum [ n + reverse(n) ] consists entirely of odd (decimal) digits. For instance, 36 + 63 = 99 and 409 + 904 = 1313. We will call such numbers reversible; so 36, 63, 409, and 904 are reversible. Leading zeroes are not allowed in either n or reverse(n). There are 120 reversible numbers below one-thousand.
  • 31. public class Reversible { final int max; final int numReversible; public Reversible(int max){ this.max = max; this.numReversible = this.countReversible(); } public int countReversible(){ if (numReversible > 0){ return numReversible; } int cnt = 0; for (int i=11;i<=max;i++){ if (reversible(i)) { cnt++; } } return cnt; } private boolean reversible(int n){ return allOdd(reverse(n)); } private boolean allOdd(int n){ String str = Integer.toString(n); for (int i=0;i<str.length();i++){ int m = Character.digit(str.charAt(i),10); if (m % 2 == 0) return false; } return true; } private int reverse(Integer n){ char[] digits = n.toString().toCharArray(); char[] rev = new char[digits.length]; for (int i=digits.length-1;i>=0;i--){ rev[i] = digits[digits.length -i-1]; } return n + Integer.parseInt(String.valueOf(rev)); } }
  • 32. public class GroovyReversible { final Integer max final Integer numReversible = 0 public GroovyReversible(max){ this.max = max this.numReversible = this.countReversible() } def countReversible(){ numReversible ?: (11..max).findAll{reversible(it)}.size() } def reversible(n){ allOdd(reverse(n)) } def allOdd(n){ n.toString().toList().collect {it.toInteger()}.every{it % 2 == 1} } def reverse(n){ n + n.toString().toList().reverse().join().toInteger() } }
  • 33. class RubyReversible def initialize(max) @max = max @numReversible = nil end def allOdd(n) digits = n.to_s.split(//) digits.length == digits.map{|c| c.to_i}.select{|i| i % 2 == 1}.length end def reverse(n) n + n.to_s.reverse.to_i end def reversible(n) allOdd(reverse(n)) end def countReversible() @numReversible ||= (11..@max).select{|i| reversible(i)}.length @numReversible end end
  • 34. class PyReversible(object): __slots__ = ['max','numReversible'] def __init__(self, max): self.max = max self.numReversible = self.countReversible() def countReversible(self): return len([i for i in xrange(11,self.max+1) if self.reversible(i)]) def allOdd(self,n): for ch in str(n): if int(ch) % 2 != 1: return False return True def reverse(self,n): return n + int(str(n)[::-1]) def reversible(self,n): return self.allOdd(self.reverse(n))
  • 35. (defn all-odd? [n] (every? odd? (map #(Integer. (str %)) (str n)))) (defn reverse-num [n] (+ n (Integer. (apply str (reverse (str n)))))) (defn reversible-num? [n] (all-odd? (reverse-num n))) (defn count-reversible [rev-max] {:max rev-max :num-reversible (count (filter reversible-num? (range 11 rev-max)))})
  • 36. class ScalaReversible(max:Int){ lazy val numReversible = (11 to max).filter(reversible(_)).size private def reverse(n:Int)=n + parseInt(n.toString.reverse) def allOdd(n:Int) = n.toString.map(digit(_,10)).forall(_ % 2 == 1) def reversible(n:Int) = allOdd(reverse(n))
  • 37. class Reversible { Int max once Int countReversible() { cnt := 0 for (i := 11; i<=max; i++) if (reversible(i)) cnt++ return cnt } private Bool reversible(Int n) { allOdd(reverse(n)) } private Bool allOdd(Int n) { n.toStr.all |char| { char.isOdd } } private Int reverse(Int n) { n + n.toStr.reverse.toInt } }
  • 38. Time / Java Time

Editor's Notes