Skip to content

Commit

Permalink
add original base64 from kostya/benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
biocyberman committed Sep 20, 2018
1 parent f7e4096 commit fbfe07d
Show file tree
Hide file tree
Showing 28 changed files with 898 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 180919_Benchmarking/codelabs/base64/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
base64_*
aklomp-base64*
50 changes: 50 additions & 0 deletions 180919_Benchmarking/codelabs/base64/Base64Java.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import java.util.Base64;
import static java.lang.System.out;
import static java.nio.charset.StandardCharsets.ISO_8859_1;

class Base64Java {

final static int STR_SIZE = 10000000;
final static int TRIES = 100;

final static Base64.Decoder dec = Base64.getDecoder();
final static Base64.Encoder enc = Base64.getEncoder();

public static void main(String[] args){

StringBuilder sb = new StringBuilder("");
for (int i = 0 ; i < STR_SIZE ; i++) {
sb.append("a");
}
String str = sb.toString();
String str2 = "";
String str3;

// cheat - JIT warming-up - 0,5-1sec
for (int i = 0 ; i < TRIES ; i++) {
enc.encodeToString(str.getBytes(ISO_8859_1)).length();
}

int s = 0;
Long t = System.nanoTime();
for (int i = 0 ; i < TRIES ; i++) {
str2 = enc.encodeToString(str.getBytes(ISO_8859_1));
s += str2.length();
}
out.println("encode: " + s + ", " + (System.nanoTime() - t)/1e9);

// cheat - JIT warming-up - 0-0,3sec
for (int i = 0 ; i < TRIES ; i++) {
dec.decode(str2.getBytes(ISO_8859_1));
}

s = 0;
t = System.nanoTime();
for (int i = 0 ; i < TRIES ; i++) {
byte[] b_arr = dec.decode(str2.getBytes(ISO_8859_1));
str3 = new String(b_arr, ISO_8859_1);
s += str3.length();
}
out.println("decode: " + s + ", " + (System.nanoTime() - t)/1e9);
}
}
8 changes: 8 additions & 0 deletions 180919_Benchmarking/codelabs/base64/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Base64 encode and decode
------------------------

Base64 is a string transformation algorithm. For most languages it is implemented in the language itself (except Ruby, Python which implemented in C), so I think it's a good benchmark for the language.

To compile all: `sh build.sh`

To run all: `sh run.sh`
32 changes: 32 additions & 0 deletions 180919_Benchmarking/codelabs/base64/Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.util.Base64

val STR_SIZE = 10000000
val TRIES = 100

val enc = Base64.getEncoder();
val dec = Base64.getDecoder();

fun main(args: Array<String>) {
val str = "a".repeat(STR_SIZE)

var count1 = 0
var encStr = ""

val t1 = System.nanoTime()
repeat(TRIES) {
encStr = enc.encodeToString(str.toByteArray())
count1 += encStr.length
}
println("encode: ${count1}, ${(System.nanoTime() - t1) / 1e9}")

var count2 = 0
var decStr: String

val t2 = System.nanoTime()
repeat(TRIES) {
val decBytes = dec.decode(encStr)
decStr = String(decBytes)
count2 += decStr.length
}
println("decode: ${count2}, ${(System.nanoTime() - t2) / 1e9}")
}
6 changes: 6 additions & 0 deletions 180919_Benchmarking/codelabs/base64/base64.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>
</Project>
1 change: 1 addition & 0 deletions 180919_Benchmarking/codelabs/base64/base64.rs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
76 changes: 76 additions & 0 deletions 180919_Benchmarking/codelabs/base64/base64.rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions 180919_Benchmarking/codelabs/base64/base64.rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "base64_rs"
version = "0.0.1"

[dependencies]
base64 = "0.9.0"
time = "0.1"

[profile.release]
lto = true
26 changes: 26 additions & 0 deletions 180919_Benchmarking/codelabs/base64/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
crystal build test.cr --release -o base64_cr --no-debug
go build -o base64_go test.go
gccgo -O3 -g -o base64_go_gccgo test.go
g++ -O3 -o base64_cpp test.cpp -lcrypto
gcc -O3 -std=c99 -o base64_c test.c
scalac -optimize test.scala
javac Base64Java.java
kotlinc Test.kt -include-runtime -d Test-kt.jar
dmd -ofbase64_d -O -release -inline test.d
gdc -o base64_d_gdc -O3 -frelease -finline test.d
ldc2 -ofbase64_d_ldc -O5 -release test.d
nim c -o:base64_nim_gcc -d:release --cc:gcc --verbosity:0 test.nim
nim c -o:base64_nim_clang -d:release --cc:clang --verbosity:0 test.nim
julia -e 'Pkg.add("Codecs")'
cargo build --manifest-path base64.rs/Cargo.toml --release && cp ./base64.rs/target/release/base64 ./base64_rs
mcs -debug- -optimize+ test.cs
dotnet build -c Release

if [ ! -d aklomp-base64-ssse ]; then
git clone --depth 1 https://github.com/aklomp/base64.git aklomp-base64-ssse
cd aklomp-base64-ssse
SSSE3_CFLAGS=-mssse3 make
cd -
fi
gcc --std=c99 -O3 test-aklomp.c -I aklomp-base64-ssse/include/ aklomp-base64-ssse/lib/libbase64.o -o base64_c_ak_ssse
wget -qO - https://cpanmin.us | perl - -L perllib MIME::Base64::Perl
12 changes: 12 additions & 0 deletions 180919_Benchmarking/codelabs/base64/clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
rm base64_*
rm *.class
rm *.o
rm *.exe
rm -rf .crystal
rm -rf nimcache
rm -rf base64.rs/target
rm -rf aklomp-base64*
rm -rf perllib
rm *.jar
rm -rf bin/
rm -rf obj/
54 changes: 54 additions & 0 deletions 180919_Benchmarking/codelabs/base64/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
echo Crystal
../xtime.rb ./base64_cr
echo Go
../xtime.rb ./base64_go
echo GccGo
../xtime.rb ./base64_go_gccgo
echo Cpp
../xtime.rb ./base64_cpp
echo C
../xtime.rb ./base64_c
echo C aklomp SSSE3
../xtime.rb ./base64_c_ak_ssse
echo Rust
../xtime.rb ./base64_rs
echo D
../xtime.rb ./base64_d
echo D Gdc
../xtime.rb ./base64_d_gdc
echo D Ldc
../xtime.rb ./base64_d_ldc
echo Nim Gcc
../xtime.rb ./base64_nim_gcc
echo Nim Clang
../xtime.rb ./base64_nim_clang
echo Julia
../xtime.rb julia test.jl
echo Scala
../xtime.rb scala Base64
echo Java
../xtime.rb java -XX:+AggressiveOpts Base64Java
echo Kotlin
../xtime.rb java -jar Test-kt.jar
echo Javascript Node
../xtime.rb node test.js
echo Python PyPy
../xtime.rb pypy test.py
echo Python
../xtime.rb python test.py
echo Python3
../xtime.rb python3 test.py
echo Ruby
../xtime.rb ruby test.rb
echo Mono
../xtime.rb mono -O=all --gc=sgen test.exe
echo C# .Net Core
../xtime.rb dotnet bin/Release/netcoreapp2.0/base64.dll
echo Perl
../xtime.rb perl -Iperllib/lib/perl5 test.pl
echo Perl XS
../xtime.rb perl test-xs.pl
echo Tcl
../xtime.rb tclsh test.tcl
echo Php
../xtime.rb php test.php
47 changes: 47 additions & 0 deletions 180919_Benchmarking/codelabs/base64/test-aklomp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "stdlib.h"
#include "stdio.h"
#include "time.h"
#include "libbase64.h"
#include "../lib/config.h"

#define FLAGS (HAVE_SSSE3) ? BASE64_FORCE_SSSE3 : 0

int main() {
const int STR_SIZE = 10000000;
const int TRIES = 100;

char *str = (char*) malloc(STR_SIZE + 1);
for (int i = 0; i < STR_SIZE; i++) { str[i] = 'a'; }
str[STR_SIZE] = '\0';

int s = 0;
clock_t t = clock();
for (int i = 0; i < TRIES; i++) {
char *str2 = (char*) malloc( (int)(STR_SIZE/3.0*4) + 6 );
size_t str2_size;
base64_encode(str, STR_SIZE, str2, &str2_size, FLAGS);
s += str2_size;
free(str2);
}
printf("encode: %d, %.2f\n", s, (float)(clock() - t)/CLOCKS_PER_SEC);

char *str2 = (char*) malloc( (int)(STR_SIZE/3.0*4) + 60 );
size_t str2_size;
base64_encode(str, STR_SIZE, str2, &str2_size, 0);

s = 0;
t = clock();
for (int i = 0; i < TRIES; i++) {
char *str3 = (char*) malloc( (int)(STR_SIZE) + 60 );
size_t str3_size;

int res = base64_decode(str2, str2_size, str3, &str3_size, FLAGS);
str3[str3_size] = '\0';
s += str3_size;
free(str3);
}
printf("decode: %d, %.2f\n", s, (float)(clock() - t)/CLOCKS_PER_SEC);

free(str);
free(str2);
}
23 changes: 23 additions & 0 deletions 180919_Benchmarking/codelabs/base64/test-xs.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use strict;
use warnings;
use MIME::Base64 qw(encode_base64 decode_base64);
use Time::HiRes 'time';

use constant STR_SIZE => 10_000_000;
use constant TRIES => 100;

my $str = 'a' x STR_SIZE;
my $str2 = '';

my ($t, $s) = (time, 0);
for (0..TRIES) {
$str2 = encode_base64 $str, '';
$s += length $str2;
}
print "encode: $s, ", (time - $t), "\n";

($t, $s) = (time, 0);
for (0..TRIES) {
$s += length decode_base64 $str2;
}
print "decode: $s, ", (time - $t), "\n";
Loading

0 comments on commit fbfe07d

Please sign in to comment.