3d graphics float "benchmarks" - Style and optimization

bearophile bearophileHUGS at lycos.com
Fri Mar 27 06:54:57 PDT 2009


I give advice to new Python programmers, they are often people coming from Java, and they write "Java in Python". One of the many kinds of things they do wrongly is to use classes for everything, and to build deep class trees. The result is code that is too much slow and long.

D language isn't much widespread, so many of new D programmers may come from Java, and D1 compiles code quite similar to basic Java. So they may write Java-style D code. I have translated naively some small programs from Java to D1 and the result is often a slow down of the running speed. This shows that D compilers have a lot of optimizations to catch up from HotSpot (I think two important "optimizations" of HotSpot are its efficient garbage collector and the ability to fully or partially inline many virtual methods).

One of those tiny 3D benchmarks shows what I mean:
http://leonardo-m.livejournal.com/79346.html

Few of those timings, seconds:
  ao_d, float:                  3.67 s
  AO.java, float, naive:        6.81 s
  ao2_py with Psyco:           16.72 s
  ao2_d, float, naive:         33.77 s

(Note that I have seen a C++ version that uses threads that runs in about 1 second on my PC, that has 2 cores, so it's about two times faster than the D code. The code is not too much different among translations because the purpose of this benchmark is to compare languages and not compare different skills in manually optimizing code).

ao_d is a fast D version of mine that uses structs and lot of references and hidden mutation. It's not easy to translate this version to "clean" and bug-free Java code because of the hidden mutations. Despite being fast I usually don't like this style of programming, because all those references and hidden mutations lead to code hard to debug and hard to translate to other languages (and sooner or later I have to translate lot of the programs I write).

AO.java is a quite naive Java adaptation of the original Processing version.

ao2_d version is a direct D translation of the Java version. It's clean, it's easy to understand, easy to debug and easy enough to translate to other languages. It shows that sometimes D code written in Java-like style can be very slow when run by D. Profiling of ao2_d D code seems to show that part of the slowdown comes from the virtual methods like dot that aren't inlined. This little program also shows why Java is so much widespread: it's easy to write correct and readable programs that aren't that much slower (and probably it's not too much difficult to add threads to this Java program, making it about as fast as my faster D version).

So D style guides have to warn new D programmers coming from Java that some of the optimizations they used to rely on, aren't available, so they have to chage their style and code if they want to keep/gain performance.

(If someone is willing I'd like to know timings for ao2_py version on a 3+ cores CPU with Psyco, and the D code run with LDC).

Bye,
bearophile



More information about the Digitalmars-d mailing list