| import "encoding/base64" | |
| import "fmt" | |
| import "time" | |
| import "strings" | |
| func main() { | |
| STR_SIZE := 10000000 | |
| TRIES := 100 | |
| str := strings.Repeat("a", STR_SIZE) | |
| str2 := "" | |
| bytes := []byte(str) | |
| coder := base64.StdEncoding | |
| t := time.Now() | |
| s := uint64(0) | |
| for i := 0; i < TRIES; i += 1 { | |
| str2 = coder.EncodeToString(bytes) | |
| s += uint64(len(str2)) | |
| } | |
| fmt.Printf("encode: %d, %.4f\n", s, float32(time.Since(t).Seconds())) | |
| t = time.Now() | |
| s = 0 | |
| for i := 0; i < TRIES; i += 1 { | |
| str3, _ := coder.DecodeString(str2) | |
| s += uint64(len(str3)) | |
| } | |
| fmt.Printf("decode: %d, %.4f\n", s, float32(time.Since(t).Seconds())) | |
| } |
| Language | Time,s | Memory, Mb |
|---|---|---|
| D Gdc | 2.48 | 44.3 |
| C | 2.70 | 32.3 |
| Ruby | 2.73 | 125.3 |
| D Ldc | 3.27 | 44.1 |
| Crystal | 3.35 | 82.4 |
| Nim | 4.13 | 52.4 |
| Ruby Rbx | 4.29 | 30.7 |
| C++ Openssl | 5.45 | 65.2 |
| D | 6.18 | 89.1 |
| Python | 7.62 | 52.6 |
| Rust | 7.40 | 42.9 |
| Javascript Node | 7.93 | 777.1 |
| Python Pypy | 8.22 | 114.6 |
| Julia | 8.91 | 378.2 |
| Ruby JRuby | 16.76 | 496.6 |
| Ruby JRuby9k | 17.72 | 417.1 |
| Go | 21.24 | 94.2 |
| Scala | 35.06 | 301.2 |
...
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);
}
}
It simply looks like the base64 code for Go has not been optimized. With some trivial changes https://gist.github.com/egonelbre/dbe66ea24edd4db6dac5 it went from:BenchmarkEncodeToString 20000 74759 ns/op 109.58 MB/sto:BenchmarkEncodeToString 20000 64458 ns/op 127.09 MB/sOf course I'm guessing there's still room for improvement.
Damian
--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
pprof shows "s = strings.Map(removeNewlinesMapper, s)" in base64.DecodeString takes 50% time to run. Simply changing it to "s = strings.Replace(strings.Replace(s, "\n", "", -1), "\r", "", -1)" will make a huge difference. So the base64 module is far from optimized.
decode: 1000000000, 28.5536
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.