memory allocation is slow, why and how to improve

%u wangyuanzju at gmail.com
Thu Oct 19 01:48:46 PDT 2006


I supposed that memory allocation in D should be very fast for it employees GC
technique. However, I dit a simple test and found D is slower that C++ and
Java. The source file a used in test is listed as follows.

== File test.d

int main(char[][] args) {
        for (int i = 0; i < 10000000; i++) {
                new byte[(i % 128) + 1];
        }
        return 0;
}

== File test.cpp
int main(void) {
        int i;
        for (i = 0; i < 10000000; i++) {
                char *p = new char[(i % 128) + 1];
                delete p;
        }
}

== File test.java
public class test {
        public static void main(String[] args) {
                for (int i = 0; i < 10000000; i++) {
                        byte[] a = new byte[(i % 128) + 1];
                }
        }
}

For test.cpp, I build it with:
  g++ -O2 test.cpp
For test.d, I build it with:
  dmd -O -release test.d

The results are:
time java test ; time ./a.out ; time ./test
real    0m7.754s
user    0m5.688s
sys     0m2.048s

real    0m11.919s
user    0m11.909s
sys     0m0.004s

real    0m19.256s
user    0m19.033s
sys     0m0.004s

You can see that Java is the fastest, D is the slowest, much slower that C++.

My test environment:
Hardware: IBM laptop T43
OS: Ubuntu Dapper 6.06
Kernel: 2.6.15-27-386
DMD version:  v0.169
g++ version: g++ (GCC) 4.0.3
java version: Sun JVM 1.5.0_06

So what's the reason, and how to improve it?



More information about the Digitalmars-d mailing list