Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Comparing the performance of d, java and cpp in microbenchmarks
C++ D Java Ruby
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
src
.classpath
.gitignore
.project
CreateDataFiles.launch
README.md
Rakefile.rb
ReadBytes1.launch
ReadBytes2.launch

README.md

Performance Microbenchmarks (CPP vs. D)

Background

While testing my protobuf binding for d I observed massive performance degradiation compared to the java protobuf binding, and when I compared with the C++ protobuf binding I run away screaming. It was many times slower than java (not to speak of cpp). I improved the d performance a lot by adjusting my code generator, but after a while I was still too slow. So I wrote some small examples, which do stress the basics of protobuf processing.

Makefile

The Makefile generates the protobuf java sources, needed for the testdata.

CreateDataFiles

This small java programm creates the datafiles in /tmp that are used by the cpp and d implementations.

ReadBytes

This set of implementations tries to read a file from /tmp as fast as possible under the constraint, that the data is pushed to the application byte by byte. This is done 10 times.

Solution bytes read (should be 128863360) Time in ms for a file of size 12886336 Description
g++-1(count)128863360115straight forward implementation using fread with buffering.
llvm-c++-1(count)128863360157straight forward implementation using fread with buffering.
g++-2(count)12886336086straight forward implementation using mmap.
llvm-c++-2(count)128863360153straight forward implementation using mmap.
g++-3(count)128863360953using plain fgetc.
llvm-c++-3(count)128863360953using plain fgetc.
g++-3(count)128863360277using plain fgetc_unlocked.
llvm-c++-3(count)128863360404using plain fgetc_unlocked.
g++-5(count)128863360116using dlopen("libc.so.6") and dlsym("fread").
llvm-c++-5(count)128863360159using dlopen("libc.so.6") and dlsym("fread").
g++-1(sum)14577336640161straight forward implementation using fread with buffering.
llvm-c++-1(sum)14577336640233straight forward implementation using fread with buffering.
g++-2(sum)14577336640153straight forward implementation using mmap.
llvm-c++-2(sum)14577336640154straight forward implementation using mmap.
g++-3(sum)14577336640990using plain fgetc.
llvm-c++-3(sum)14577336640968using plain fgetc.
g++-3(sum)14577336640281using plain fgetc_unlocked.
llvm-c++-3(sum)14577336640627using plain fgetc_unlocked.
g++-5(sum)14577336640170using dlopen("libc.so.6") and dlsym("fread").
llvm-c++-5(sum)14577336640211using dlopen("libc.so.6") and dlsym("fread").
d2-Digital Mars D-1(count)1288633601802using std.stream.BufferedFile with ubyte[1].
d2-LDC-1(count)12886336012515using std.stream.BufferedFile with ubyte[1].
d2-GDC-1(count)1288633601504using std.stream.BufferedFile with ubyte[1].
d2-Digital Mars D-2(count)128863360546using std.c.stdio.fread with buffering (returning nr of bytes and taking a naked ptr for the result).
d2-LDC-2(count)128863360115using std.c.stdio.fread with buffering (returning nr of bytes and taking a naked ptr for the result).
d2-GDC-2(count)128863360116using std.c.stdio.fread with buffering (returning nr of bytes and taking a naked ptr for the result).
d2-Digital Mars D-3(count)128863360498using std.c.stdio.fread with buffering (result is false for eof and returnvalue is an out-param).
d2-LDC-3(count)128863360117using std.c.stdio.fread with buffering (result is false for eof and returnvalue is an out-param).
d2-GDC-3(count)128863360116using std.c.stdio.fread with buffering (result is false for eof and returnvalue is an out-param).
d2-Digital Mars D-4(count)128863360496using std.c.stdio.fread with a semantic like java (return -1 for eof).
d2-LDC-4(count)128863360333using std.c.stdio.fread with a semantic like java (return -1 for eof).
d2-GDC-4(count)128863360116using std.c.stdio.fread with a semantic like java (return -1 for eof).
d2-Digital Mars D-5(count)128863360486using mmfile and a direct pointer to the slice.
d2-LDC-5(count)128863360149using mmfile and a direct pointer to the slice.
d2-GDC-5(count)12886336085using mmfile and a direct pointer to the slice.
d2-Digital Mars D-6(count)128863360596using std.stdio.File with arrayslicing.
d2-LDC-6(count)128863360334using std.stdio.File with arrayslicing.
d2-GDC-6(count)128863360160using std.stdio.File with arrayslicing.
d2-Digital Mars D-7(count)128863360740using dlopen("libc") and dlsym("fread").
d2-LDC-7(count)128863360115using dlopen("libc") and dlsym("fread").
d2-GDC-7(count)128863360115using dlopen("libc") and dlsym("fread").
d2-Digital Mars D-1(sum)145773366401790using std.stream.BufferedFile with ubyte[1].
d2-LDC-1(sum)1457733664012541using std.stream.BufferedFile with ubyte[1].
d2-GDC-1(sum)145773366401546using std.stream.BufferedFile with ubyte[1].
d2-Digital Mars D-2(sum)14577336640720using std.c.stdio.fread with buffering (returning nr of bytes and taking a naked ptr for the result).
d2-LDC-2(sum)14577336640157using std.c.stdio.fread with buffering (returning nr of bytes and taking a naked ptr for the result).
d2-GDC-2(sum)14577336640157using std.c.stdio.fread with buffering (returning nr of bytes and taking a naked ptr for the result).
d2-Digital Mars D-3(sum)14577336640741using std.c.stdio.fread with buffering (result is false for eof and returnvalue is an out-param).
d2-LDC-3(sum)14577336640157using std.c.stdio.fread with buffering (result is false for eof and returnvalue is an out-param).
d2-GDC-3(sum)14577336640160using std.c.stdio.fread with buffering (result is false for eof and returnvalue is an out-param).
d2-Digital Mars D-4(sum)14577336640455using std.c.stdio.fread with a semantic like java (return -1 for eof).
d2-LDC-4(sum)14577336640297using std.c.stdio.fread with a semantic like java (return -1 for eof).
d2-GDC-4(sum)14577336640160using std.c.stdio.fread with a semantic like java (return -1 for eof).
d2-Digital Mars D-5(sum)14577336640443using mmfile and a direct pointer to the slice.
d2-LDC-5(sum)14577336640190using mmfile and a direct pointer to the slice.
d2-GDC-5(sum)14577336640106using mmfile and a direct pointer to the slice.
d2-Digital Mars D-6(sum)14577336640669using std.stdio.File with arrayslicing.
d2-LDC-6(sum)14577336640336using std.stdio.File with arrayslicing.
d2-GDC-6(sum)14577336640202using std.stdio.File with arrayslicing.
d2-Digital Mars D-7(sum)14577336640732using dlopen("libc") and dlsym("fread").
d2-LDC-7(sum)14577336640157using dlopen("libc") and dlsym("fread").
d2-GDC-7(sum)14577336640160using dlopen("libc") and dlsym("fread").
java-11288633603025using BufferedInputStream.read().
java-2128863360339using a read into a buffer[1] with buffering.
java-3128863360261using nio and terminating with BufferUnderflowException.

Outlook

As you can see, most of the d solutions are slower than java or c++. Only if you choose the right implementation for the right compiler similar results to g++ can be expected. Please help me improve / enhance the solutions.

Something went wrong with that request. Please try again.