FAQ
I gave the Java version some more memory (VM options -server -Xss15500k)
and all of a sudden it had about the same performance as Scala. Looks like
the Scala for loop creates less garbage.

Jour Java code with more memory and using Go 1.4 I now get on my other
machine:

Go:

encode: 1333333600, 8.0265

decode: 1000000000, 28.5536

Java:

encode: 1333333600, 3.185907024
decode: 1000000000, 7.416037981

So things changed again ...


Am Montag, 13. April 2015 18:42:27 UTC+2 schrieb Konstantin Shaposhnikov:
Just want to make a few notes on the Java/Scala benchmark mostly to
demonstrate that benchmarks like this not that useful/accurate.

1. The Java code that you provided is not exact equivalent of the original
Scala code, str variable that is declared as val in Scala should be
declared final in Java (so Java will optimize str.getBytes() to be called
only once):

final String str = ""

2. When micro benchmarking Java you should generally warm up the hotspot
compiler by calling the method that you are benchmarking a few times.
Otherwise you are benchmarking un-optimized interpreted version of the
method. Even better use a benchmarking framework (e.g. JMH).

3. There is a bug in both Scala and Java benchmark code, t is not
initialized before decode.

4. Finally C version and Java/Scala version are not exactly equivalent as
Java/Scala version in addition to base64 also converts result to String.

The final benchmark that takes all above points in consideration completes
in 2/4.5 seconds for encode/decode instead of original 27/16 seconds on my
laptop):

import java.io.IOException;
import java.util.Base64;

public class Base64Perf {

public static void main(String[] args) throws IOException {
long s = 0;

Base64.Encoder enc = Base64.getEncoder();
// warm up
for (int i = 0; i < 10000; i++) {
s += enc.encode(new byte[] { 1 }).length;
}

Base64.Decoder dec = Base64.getDecoder();
for (int i = 0; i < 10000; i++) {
s += dec.decode("MQ==".getBytes()).length;
}

int STR_SIZE = 10000000;
int TRIES = 100;

byte[] bytes = new byte[STR_SIZE];
for (int i = 0; i < STR_SIZE; i++) {
bytes[i] = 'a';
}
byte[] bytes2 = null;

long t = System.nanoTime();
s = 0;
for (int i = 0; i < TRIES; i++) {
bytes2 = enc.encode(bytes);
s += bytes2.length;
}

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

t = System.nanoTime();
s = 0;

for (int i = 0; i < TRIES; i++) {
byte[] bytes3 = dec.decode(bytes2);
s += bytes3.length;
}

System.out.println("decode: " + s + ", " + (System.nanoTime() -
t)/1e9);
}
}
On Monday, 13 April 2015 16:43:45 UTC+8, Haddock wrote:

I quickly ported the Scala code to Java (Java code at the end of the
post). On my machine (Core i7-4600M, 2.90 GHz, quad-core, Windows 7) I get
these results:

Go 1.4.1:

encode: 1333333600, 3.4110
decode: 1000000000, 15.3430

Scala (JDK8):

encode: 1368421200, 13.783206397
decode: 1000000000, 26.121356501

Java (JDK8):

encode: 1368421200, 31.008432298
decode: 1000000000, 58.633304231


So Go performs remarkably better than Scala. Quite astonishing that Java
is so much slower than Scala. To me Go is doing really well compared to
other languages that also don't call C (admittedly, I don't exactly know
what the Java classes BASE64Encoder, BASE64Decoder are exactly doing).

-- H.

Java code:

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

import java.io.IOException;

/**
* Created by plohmann on 13.04.2015.
*/
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 "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 12 of 19 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 13, '15 at 1:36a
activeJun 22, '15 at 2:19p
posts19
users10
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase