FAQ
Hello,

I recently discovered this little simple benchmark
<https://github.com/kostya/benchmarks/tree/master/base64>. I made a quick
port of the Scala code to Java (code at the end of the post). Here are the
measurements on my machine (Core i7-4600M, 2.90 GHz, quad-core, Windows 7)
I get these results:

Scala (JDK8):

encode: 1368421200, 13.783206397 s
decode: 1000000000, 26.121356501 s

Java (JDK8):

encode: 1368421200, 31.008432298 s
decode: 1000000000, 58.633304231 s

I just wonder how comes Scala is so much faster. I mean it only calls the
Java BASE64Encoder and BASE64Decoder just like Java. Maybe it is obvious,
but I'm not proficient enough in Scala to see what makes the difference to
the Java version. If some could shed some light on this for me to grasp
this :-).

Thanks, Haddock


Java code:

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;


public class Base64 {

     public static void main(String[] args) throws IOException {

         BASE64Encoder enc = new sun.misc.BASE64Encoder();
         BASE64Decoder dec = new sun.misc.BASE64Decoder();

         int STR_SIZE = 10000000;
         int TRIES = 100;

         StringBuffer buffer = new StringBuffer();
         for (int i = 0; i < STR_SIZE; i++) {
             buffer.append("a");
         }
         String str = buffer.toString();
         String str2 = "";
         long t = System.nanoTime();
         long s = 0;

         for (int i = 0; i < TRIES; i++) {
             str2 = enc.encode(str.getBytes());
             s += str2.length();
         }

         System.out.println("encode: " + s + ", " + (System.nanoTime() -
t)/1e9);
         s = 0;

         for (int i = 0; i < TRIES; i++) {
             byte[] str3 = dec.decodeBuffer(str2);
             s += str3.length;
         }

         System.out.println("decode: " + s + ", " + (System.nanoTime() -
t)/1e9);
     }
}

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 4 | next ›
Discussion Overview
groupscala-user @
categoriesscala
postedApr 13, '15 at 10:21a
activeApr 13, '15 at 3:54p
posts4
users2
websitescala-lang.org
irc#scala

2 users in discussion

Haddock: 3 posts Will Sargent: 1 post

People

Translate

site design / logo © 2022 Grokbase