Skip to content

Instantly share code, notes, and snippets.

@mollymorphic
Last active August 29, 2015 14:01
Show Gist options
  • Save mollymorphic/8237add9b7c9eeede54b to your computer and use it in GitHub Desktop.
Save mollymorphic/8237add9b7c9eeede54b to your computer and use it in GitHub Desktop.
D vs Java - some benchmarks comparing D and Java in allocating dynamically sized arrays
/**
bench.d -
Allocation/Garbage collection benchmark for D
*/
import std.stdio;
import std.datetime;
import std.conv;
ulong[] ascendingAlloc(int n, int max_size, bool warmup)
{
// setup timer
StopWatch sw;
TickDuration[] times;
times.length = n;
TickDuration last = TickDuration.from!"seconds"(0);
sw.start();
// microbenchmark code
ulong[] lens;
for (auto i = 0; i < n; i++)
{
double[][] x;
auto ii = 0;
while (ii++ < max_size)
{
double[] y;
y.length = ii;
x ~= y;
}
lens ~= x.length;
ii = max_size;
while (ii-- > 1)
{
x.length = ii;
}
x.length = 0;
}
// stop timer
sw.stop();
auto t = sw.peek() - last;
// output results
if (warmup == false)
writeln("ascendingAllocD,", t.msecs);
return lens;
}
ulong[] descendingAlloc(int n, int max_size, bool warmup)
{
// setup timer
StopWatch sw;
TickDuration[] times;
times.length = n;
TickDuration last = TickDuration.from!"seconds"(0);
sw.start();
// microbenchmark code
ulong[] lens;
for (auto i = 0; i < n; i++)
{
double[][] x;
auto ii = max_size;
while (ii-- > 1)
{
double[] y;
y.length = ii;
x ~= y;
}
lens ~= x.length;
ii = max_size;
while (ii-- > 1)
{
x.length = ii;
}
x.length = 0;
}
// stop timer
sw.stop();
auto t = sw.peek() - last;
// output results
if (warmup == false)
writeln("descendingAllocD,", t.msecs);
return lens;
}
ulong[] alternatingAlloc(int n, int max_size, bool warmup)
{
// setup timer
StopWatch sw;
TickDuration[] times;
times.length = n;
TickDuration last = TickDuration.from!"seconds"(0);
sw.start();
// microbenchmark code
ulong[] lens;
for (auto i = 0; i < n; i++)
{
double[][] x;
auto ii = 0;
int l1 = max_size / 2;
int l2 = 0;
int limit = l1;
while (ii++ < limit)
{
double[] y;
double[] y2;
y.length = ++l1;
y2.length = ++l2;
x ~= y;
x ~= y2;
}
lens ~= x.length;
ii = limit;
while (ii-- > 1)
{
x.length = ii;
}
x.length = 0;
}
// stop timer
sw.stop();
auto t = sw.peek() - last;
// output results
if (warmup == false)
writeln("alternatingAllocD,", t.msecs);
return lens;
}
void main(string[] args)
{
auto bench_iters = to!int(args[1]);
auto times = to!int(args[2]);
auto max_size = to!int(args[3]);
//warmup
auto warmup = ascendingAlloc(times, max_size, true);
warmup = ascendingAlloc(times, max_size, true);
warmup = alternatingAlloc(times, max_size, true);
warmup = ascendingAlloc(times, max_size, true);
warmup = ascendingAlloc(times, max_size, true);
warmup = alternatingAlloc(times, max_size, true);
// run benchmarks
while (bench_iters-- > 0)
{
auto result = ascendingAlloc(times, max_size, false);
auto result2 = descendingAlloc(times, max_size, false);
auto result3 = alternatingAlloc(times, max_size, false);
auto result4 = alternatingAlloc(times, max_size, false);
auto result5 = descendingAlloc(times, max_size, false);
auto result6 = ascendingAlloc(times, max_size, false);
auto result7 = ascendingAlloc(times, max_size, false);
auto result8 = descendingAlloc(times, max_size, false);
auto result9 = alternatingAlloc(times, max_size, false);
max_size += 1000;
}
}
/**
Bench.java -
Allocation/Garbage collection benchmark for Java
*/
import java.util.ArrayList;
import java.util.Collections;
/**
*
* @author tommy
*/
public class Bench {
public static ArrayList<Integer> ascendingAlloc(int n, int max_size, boolean warmup) {
ArrayList<Integer> lens = new ArrayList<Integer>();
// setup timer
long startTime = System.currentTimeMillis();
// microbenchmark code
for (int i = 0; i < n; i++)
{
ArrayList<ArrayList<Double>> x = new ArrayList<ArrayList<Double>>();
int ii = 0;
while (ii++ < max_size)
{
ArrayList<Double> y = new ArrayList<Double>(ii);
x.add(y);
}
lens.add(x.size());
ii = max_size;
while (ii-- > 1)
{
x.remove(ii - 1);
}
x.clear();
}
// stop timer
long estimatedTime = System.currentTimeMillis() - startTime;
// output results
if (warmup == false)
{
System.out.print("ascendingAllocJava,");
System.out.print(estimatedTime);
System.out.print('\n');
}
return lens;
}
public static ArrayList<Integer> descendingAlloc(int n, int max_size, boolean warmup) {
ArrayList<Integer> lens = new ArrayList<Integer>();
// setup timer
long startTime = System.currentTimeMillis();
// microbenchmark code
for (int i = 0; i < n; i++)
{
ArrayList<ArrayList<Double>> x = new ArrayList<ArrayList<Double>>();
int ii = max_size;
while (ii-- > 1)
{
ArrayList<Double> y = new ArrayList<Double>(ii);
x.add(y);
}
lens.add(x.size());
ii = max_size;
while (ii-- > 1)
{
x.remove(ii - 1);
}
x.clear();
}
// stop timer
long estimatedTime = System.currentTimeMillis() - startTime;
// output results
if (warmup == false)
{
System.out.print("descendingAllocJava,");
System.out.print(estimatedTime);
System.out.print('\n');
}
return lens;
}
public static ArrayList<Integer> alternatingAlloc(int n, int max_size, boolean warmup) {
ArrayList<Integer> lens = new ArrayList<Integer>();
// setup timer
long startTime = System.currentTimeMillis();
// microbenchmark code
for (int i = 0; i < n; i++)
{
ArrayList<ArrayList<Double>> x = new ArrayList<ArrayList<Double>>();
int ii = 0;
int l1 = max_size / 2;
int l2 = 0;
int limit = l1;
while (ii++ < limit)
{
ArrayList<Double> y = new ArrayList<Double>(++l1);
ArrayList<Double> y2 = new ArrayList<Double>(++l2);
x.add(y);
x.add(y2);
}
lens.add(x.size());
ii = limit;
while (ii-- > 1)
{
x.remove(ii - 1);
}
x.clear();
}
// stop timer
long estimatedTime = System.currentTimeMillis() - startTime;
// output results
if (warmup == false)
{
System.out.print("alternatingAllocJava,");
System.out.print(estimatedTime);
System.out.print('\n');
}
return lens;
}
/**
* @param args bench_iters times max_size
*/
public static void main(String[] args) {
int bench_iters = Integer.parseInt(args[0]);
int times = Integer.parseInt(args[1]);
int max_size = Integer.parseInt(args[2]);
// warmup
ArrayList<Integer> warmup = ascendingAlloc(times, max_size, true);
warmup = ascendingAlloc(times, max_size, true);
warmup = descendingAlloc(times, max_size, true);
warmup = descendingAlloc(times, max_size, true);
warmup = alternatingAlloc(times, max_size, true);
warmup = alternatingAlloc(times, max_size, true);
// run benchmarks
while (bench_iters-- > 0)
{
ArrayList<Integer> result = ascendingAlloc(times, max_size, false);
ArrayList<Integer> result2 = descendingAlloc(times, max_size, false);
ArrayList<Integer> result3 = alternatingAlloc(times, max_size, false);
ArrayList<Integer> result4 = descendingAlloc(times, max_size, false);
ArrayList<Integer> result5 = ascendingAlloc(times, max_size, false);
ArrayList<Integer> result6 = alternatingAlloc(times, max_size, false);
ArrayList<Integer> result7 = alternatingAlloc(times, max_size, false);
ArrayList<Integer> result8 = ascendingAlloc(times, max_size, false);
ArrayList<Integer> result9 = descendingAlloc(times, max_size, false);
max_size += 1000;
}
}
}
ascendingAllocD 33862
descendingAllocD 32483
alternatingAllocD 32585
alternatingAllocD 32546
descendingAllocD 32802
ascendingAllocD 32242
ascendingAllocD 32264
descendingAllocD 32310
alternatingAllocD 32299
ascendingAllocD 126195
descendingAllocD 125532
alternatingAllocD 122514
alternatingAllocD 123899
descendingAllocD 126156
ascendingAllocD 123129
ascendingAllocD 122846
descendingAllocD 124337
alternatingAllocD 122758
ascendingAllocD 278204
descendingAllocD 273561
alternatingAllocD 281794
alternatingAllocD 284451
descendingAllocD 336018
ascendingAllocD 277391
ascendingAllocD 276739
descendingAllocD 294392
alternatingAllocD 278840
ascendingAllocD 567217
descendingAllocD 481567
alternatingAllocD 501457
alternatingAllocD 502602
descendingAllocD 507789
ascendingAllocD 480667
ascendingAllocD 484379
descendingAllocD 485905
alternatingAllocD 477125
ascendingAllocD 763838
descendingAllocD 749626
alternatingAllocD 789073
alternatingAllocD 786492
descendingAllocD 809695
ascendingAllocD 773798
ascendingAllocD 764698
descendingAllocD 747399
alternatingAllocD 747012
ascendingAllocJava 6005
descendingAllocJava 5925
alternatingAllocJava 7402
descendingAllocJava 5960
ascendingAllocJava 5998
alternatingAllocJava 7393
alternatingAllocJava 7395
ascendingAllocJava 5992
descendingAllocJava 5916
ascendingAllocJava 23135
descendingAllocJava 23107
alternatingAllocJava 28703
descendingAllocJava 23085
ascendingAllocJava 23209
alternatingAllocJava 28715
alternatingAllocJava 28710
ascendingAllocJava 23232
descendingAllocJava 23097
ascendingAllocJava 53390
descendingAllocJava 52874
alternatingAllocJava 65721
descendingAllocJava 52855
ascendingAllocJava 53479
alternatingAllocJava 64889
alternatingAllocJava 64816
ascendingAllocJava 52602
descendingAllocJava 51991
ascendingAllocJava 96741
descendingAllocJava 96272
alternatingAllocJava 118131
descendingAllocJava 96169
ascendingAllocJava 96673
alternatingAllocJava 118067
alternatingAllocJava 118054
ascendingAllocJava 96728
descendingAllocJava 96283
ascendingAllocJava 175425
descendingAllocJava 172225
alternatingAllocJava 205451
descendingAllocJava 181726
ascendingAllocJava 170136
alternatingAllocJava 211307
alternatingAllocJava 208900
ascendingAllocJava 170157
descendingAllocJava 175671
#!/bin/bash
## run the d benchmarks and java benchmarks
## results included run with ./run.sh 5 6000 1000
if [ $# -lt 3 ]
then
echo "usage: ./run.sh (bench_iters) (times) (maxsize start)"
exit 1;
fi
rm -f ./dresults.csv;
dmd -m64 -O -inline -noboundscheck -ofbench bench.d
./bench $* > dresults.csv;
rm -f *.o;
rm -f bench;
rm -f ./javaresults.csv;
javac Bench.java;
java -server Bench $* > javaresults.csv;
rm *.class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment