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

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 ContextHendrik Drachsler
 
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 unit3Terri Weiss
 
Dales Garden 5 10 08 Presentation4
Dales Garden 5 10 08 Presentation4Dales Garden 5 10 08 Presentation4
Dales Garden 5 10 08 Presentation4themir
 
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? :PLuciano Rocha
 
Kulturkartläggnings processen
Kulturkartläggnings  processenKulturkartläggnings  processen
Kulturkartläggnings processenLina Ydrefelt
 
Open Education and Sustainability
Open Education and SustainabilityOpen Education and Sustainability
Open Education and SustainabilityJoss Winn
 
Collaborative Assessment
Collaborative AssessmentCollaborative Assessment
Collaborative AssessmentJennifer Orr
 
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 Merkezianttab
 
新聞整理五則
新聞整理五則新聞整理五則
新聞整理五則guest370229
 
Antelope by Kijana
Antelope by KijanaAntelope by Kijana
Antelope by Kijanavebrya
 
Phoenix Grid Alternatives Solar Rehabilitation Presentation
Phoenix   Grid Alternatives Solar Rehabilitation PresentationPhoenix   Grid Alternatives Solar Rehabilitation Presentation
Phoenix Grid Alternatives Solar Rehabilitation PresentationICF_HCD
 
生命を理解する道具としての計算機  SCSN@UCLA
生命を理解する道具としての計算機  SCSN@UCLA生命を理解する道具としての計算機  SCSN@UCLA
生命を理解する道具としての計算機  SCSN@UCLAKeiichiro Ono
 

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

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 schoolMichael Galpin
 
Design Patterns for Tablets and Smartphones
Design Patterns for Tablets and SmartphonesDesign Patterns for Tablets and Smartphones
Design Patterns for Tablets and SmartphonesMichael Galpin
 
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 TechnologiesMichael Galpin
 
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 BatteryMichael Galpin
 
Persistent Data Structures And Managed References
Persistent Data Structures And Managed ReferencesPersistent Data Structures And Managed References
Persistent Data Structures And Managed ReferencesMichael Galpin
 
Mobile Development 101
Mobile Development 101Mobile Development 101
Mobile Development 101Michael Galpin
 
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 GWTMichael Galpin
 
Introduction to Scala for Java Developers
Introduction to Scala for Java DevelopersIntroduction to Scala for Java Developers
Introduction to Scala for Java DevelopersMichael 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

Escort Service in Al Qusais +971509530047 UAE
Escort Service in Al Qusais +971509530047 UAEEscort Service in Al Qusais +971509530047 UAE
Escort Service in Al Qusais +971509530047 UAEvecevep119
 
San Jon Motel, Motel/Residence, San Jon, NM
San Jon Motel, Motel/Residence, San Jon, NMSan Jon Motel, Motel/Residence, San Jon, NM
San Jon Motel, Motel/Residence, San Jon, NMroute66connected
 
The Masque of the Red Death Storyboard 2023
The Masque of the Red Death Storyboard 2023The Masque of the Red Death Storyboard 2023
The Masque of the Red Death Storyboard 2023magalybtapia
 
Element of art, Transcreation and usions and overlapping and interrelated ele...
Element of art, Transcreation and usions and overlapping and interrelated ele...Element of art, Transcreation and usions and overlapping and interrelated ele...
Element of art, Transcreation and usions and overlapping and interrelated ele...jheramypagoyoiman801
 
Lost Keys Storyboard - Randomized Timed Exercise
Lost Keys Storyboard - Randomized Timed ExerciseLost Keys Storyboard - Randomized Timed Exercise
Lost Keys Storyboard - Randomized Timed Exercisemagalybtapia
 
Escort Service in Abu Dhabi +971509530047 UAE
Escort Service in Abu Dhabi +971509530047 UAEEscort Service in Abu Dhabi +971509530047 UAE
Escort Service in Abu Dhabi +971509530047 UAEvecevep119
 
Olympia Cafe, Restaurants-cafes, Albuquerque, NM
Olympia Cafe, Restaurants-cafes, Albuquerque, NMOlympia Cafe, Restaurants-cafes, Albuquerque, NM
Olympia Cafe, Restaurants-cafes, Albuquerque, NMroute66connected
 
Lindy's Coffee Shop, Restaurants-cafes, Albuquerque, NM
Lindy's Coffee Shop, Restaurants-cafes, Albuquerque, NMLindy's Coffee Shop, Restaurants-cafes, Albuquerque, NM
Lindy's Coffee Shop, Restaurants-cafes, Albuquerque, NMroute66connected
 
Roadrunner Motel, Motel/Residence. Tucumcari, NM
Roadrunner Motel, Motel/Residence. Tucumcari, NMRoadrunner Motel, Motel/Residence. Tucumcari, NM
Roadrunner Motel, Motel/Residence. Tucumcari, NMroute66connected
 
Americana Motel, Motel/Residence, Tucumcari, NM
Americana Motel, Motel/Residence, Tucumcari, NMAmericana Motel, Motel/Residence, Tucumcari, NM
Americana Motel, Motel/Residence, Tucumcari, NMroute66connected
 
Escort Service in Al Rigga +971509530047 UAE
Escort Service in Al Rigga +971509530047 UAEEscort Service in Al Rigga +971509530047 UAE
Escort Service in Al Rigga +971509530047 UAEvecevep119
 
STAR Scholars Program Brand Guide Presentation
STAR Scholars Program Brand Guide PresentationSTAR Scholars Program Brand Guide Presentation
STAR Scholars Program Brand Guide Presentationmakaiodm
 
Zoo_championship_Wrestling (action/comedy sample)
Zoo_championship_Wrestling (action/comedy sample)Zoo_championship_Wrestling (action/comedy sample)
Zoo_championship_Wrestling (action/comedy sample)DavonBrooks
 
Indian Escorts In Al Mankhool 0509430017 Escort Agency in Al Mankhool
Indian Escorts In Al Mankhool 0509430017 Escort Agency in Al MankhoolIndian Escorts In Al Mankhool 0509430017 Escort Agency in Al Mankhool
Indian Escorts In Al Mankhool 0509430017 Escort Agency in Al Mankhoolqueenbanni425
 
Hiway Motel, Motel/Residence, Albuquerque NM
Hiway Motel, Motel/Residence, Albuquerque NMHiway Motel, Motel/Residence, Albuquerque NM
Hiway Motel, Motel/Residence, Albuquerque NMroute66connected
 
New_Cross_Over (Comedy storyboard sample)
New_Cross_Over (Comedy storyboard sample)New_Cross_Over (Comedy storyboard sample)
New_Cross_Over (Comedy storyboard sample)DavonBrooks
 
Bobbie goods colorinsssssssssssg book.pdf
Bobbie goods colorinsssssssssssg book.pdfBobbie goods colorinsssssssssssg book.pdf
Bobbie goods colorinsssssssssssg book.pdflunavro0105
 
Kristy Soto's Industrial design Portfolio
Kristy Soto's Industrial design PortfolioKristy Soto's Industrial design Portfolio
Kristy Soto's Industrial design PortfolioKristySoto
 
Bai tap thuc hanh Anh 6 Mai Lan Huong.docx
Bai tap thuc hanh Anh 6 Mai Lan Huong.docxBai tap thuc hanh Anh 6 Mai Lan Huong.docx
Bai tap thuc hanh Anh 6 Mai Lan Huong.docxbichthuyt81
 
UNIT 5-6 anh văn chuyên nganhhhhhhh.docx
UNIT 5-6 anh văn chuyên nganhhhhhhh.docxUNIT 5-6 anh văn chuyên nganhhhhhhh.docx
UNIT 5-6 anh văn chuyên nganhhhhhhh.docxssuser519b4b
 

Recently uploaded (20)

Escort Service in Al Qusais +971509530047 UAE
Escort Service in Al Qusais +971509530047 UAEEscort Service in Al Qusais +971509530047 UAE
Escort Service in Al Qusais +971509530047 UAE
 
San Jon Motel, Motel/Residence, San Jon, NM
San Jon Motel, Motel/Residence, San Jon, NMSan Jon Motel, Motel/Residence, San Jon, NM
San Jon Motel, Motel/Residence, San Jon, NM
 
The Masque of the Red Death Storyboard 2023
The Masque of the Red Death Storyboard 2023The Masque of the Red Death Storyboard 2023
The Masque of the Red Death Storyboard 2023
 
Element of art, Transcreation and usions and overlapping and interrelated ele...
Element of art, Transcreation and usions and overlapping and interrelated ele...Element of art, Transcreation and usions and overlapping and interrelated ele...
Element of art, Transcreation and usions and overlapping and interrelated ele...
 
Lost Keys Storyboard - Randomized Timed Exercise
Lost Keys Storyboard - Randomized Timed ExerciseLost Keys Storyboard - Randomized Timed Exercise
Lost Keys Storyboard - Randomized Timed Exercise
 
Escort Service in Abu Dhabi +971509530047 UAE
Escort Service in Abu Dhabi +971509530047 UAEEscort Service in Abu Dhabi +971509530047 UAE
Escort Service in Abu Dhabi +971509530047 UAE
 
Olympia Cafe, Restaurants-cafes, Albuquerque, NM
Olympia Cafe, Restaurants-cafes, Albuquerque, NMOlympia Cafe, Restaurants-cafes, Albuquerque, NM
Olympia Cafe, Restaurants-cafes, Albuquerque, NM
 
Lindy's Coffee Shop, Restaurants-cafes, Albuquerque, NM
Lindy's Coffee Shop, Restaurants-cafes, Albuquerque, NMLindy's Coffee Shop, Restaurants-cafes, Albuquerque, NM
Lindy's Coffee Shop, Restaurants-cafes, Albuquerque, NM
 
Roadrunner Motel, Motel/Residence. Tucumcari, NM
Roadrunner Motel, Motel/Residence. Tucumcari, NMRoadrunner Motel, Motel/Residence. Tucumcari, NM
Roadrunner Motel, Motel/Residence. Tucumcari, NM
 
Americana Motel, Motel/Residence, Tucumcari, NM
Americana Motel, Motel/Residence, Tucumcari, NMAmericana Motel, Motel/Residence, Tucumcari, NM
Americana Motel, Motel/Residence, Tucumcari, NM
 
Escort Service in Al Rigga +971509530047 UAE
Escort Service in Al Rigga +971509530047 UAEEscort Service in Al Rigga +971509530047 UAE
Escort Service in Al Rigga +971509530047 UAE
 
STAR Scholars Program Brand Guide Presentation
STAR Scholars Program Brand Guide PresentationSTAR Scholars Program Brand Guide Presentation
STAR Scholars Program Brand Guide Presentation
 
Zoo_championship_Wrestling (action/comedy sample)
Zoo_championship_Wrestling (action/comedy sample)Zoo_championship_Wrestling (action/comedy sample)
Zoo_championship_Wrestling (action/comedy sample)
 
Indian Escorts In Al Mankhool 0509430017 Escort Agency in Al Mankhool
Indian Escorts In Al Mankhool 0509430017 Escort Agency in Al MankhoolIndian Escorts In Al Mankhool 0509430017 Escort Agency in Al Mankhool
Indian Escorts In Al Mankhool 0509430017 Escort Agency in Al Mankhool
 
Hiway Motel, Motel/Residence, Albuquerque NM
Hiway Motel, Motel/Residence, Albuquerque NMHiway Motel, Motel/Residence, Albuquerque NM
Hiway Motel, Motel/Residence, Albuquerque NM
 
New_Cross_Over (Comedy storyboard sample)
New_Cross_Over (Comedy storyboard sample)New_Cross_Over (Comedy storyboard sample)
New_Cross_Over (Comedy storyboard sample)
 
Bobbie goods colorinsssssssssssg book.pdf
Bobbie goods colorinsssssssssssg book.pdfBobbie goods colorinsssssssssssg book.pdf
Bobbie goods colorinsssssssssssg book.pdf
 
Kristy Soto's Industrial design Portfolio
Kristy Soto's Industrial design PortfolioKristy Soto's Industrial design Portfolio
Kristy Soto's Industrial design Portfolio
 
Bai tap thuc hanh Anh 6 Mai Lan Huong.docx
Bai tap thuc hanh Anh 6 Mai Lan Huong.docxBai tap thuc hanh Anh 6 Mai Lan Huong.docx
Bai tap thuc hanh Anh 6 Mai Lan Huong.docx
 
UNIT 5-6 anh văn chuyên nganhhhhhhh.docx
UNIT 5-6 anh văn chuyên nganhhhhhhh.docxUNIT 5-6 anh văn chuyên nganhhhhhhh.docx
UNIT 5-6 anh văn chuyên nganhhhhhhh.docx
 

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