Friday, December 24, 2010

Java vs C++ speed comparison for FFT and DFT

I am planning to make a software defined radio (SDR) transceiver which will do all the baseband processing in software. I might use FFT to convert the signals to the frequency domain then filter them.

Common knowledge has it that, for performance reasons, DSP code should be written in C/C++. My recent programming experience has mostly been in Java.  I haven't done serious C++ coding for quite a while and my C coding has been limited to microcontroller programming.  So before embarking on the SDR project, i thought i would do some speed comparisons between Java and C++.
I planned to stick with Java if its speed wasn't too slow (like 4096 samples FFT in a few milliseconds) but planned to move to C++ and the Qt framework if Java performance really sucked. I wanted to do the tests with everything out of the box i.e. without wasting time optimizing either the compiler and runtime options (of which i know nothing anyway) or the code.  The FFT code is from Richard Baldwin's online DSP tutorial (these tuts explain DSP is a really intuitive way without getting bogged down in the math).

I was surprised by the results. Java seems to be a bit faster than C++ on my Windows 7 machine (setup given below). On the other hand, C++ seemed to be a bit faster than Java on a Dell XPS 1530 laptop running Fedora 13 linux (though i didn't record the test results from the F13 machine). Either way, the results are close so i don't think i will need to move to C++/Qt for my forthcoming project.

Testbed -

JVM:     1.6.0_22    32bit           
gcc:        MinGw-TDM 4.5.1   32bit
CPU:      i5-520M  2.4 GHz
RAM:     4GB
OS:        Windows 7   64bit
Laptop:   Asus N61Jv
IDE:       NetBeans 6.9.1

Results -

The results and the source code used for the test can be found here.

1 comment:

  1. I think that the measured time is near the sampling limit so there is probably some aliasing going.

    I ran a test, increasing 'len', but when it gets bigger, i noticed that the gc time is more pronounced. The C++ version does not use dynamic memory allocation, so I removed that from java.

    System.arraycopy(proto, 0, data, 0, len);

    With len = 131072*4, the average time was:
    java: 336 ms
    c++ : 319 ms

    Still not bad.