Page 1 of 1

Compiler and program speed

Posted: Wed Jun 20, 2012 5:51 pm
by WebDrake
I've had a fun few days playing around with the Pi -- setting up Debian squeeze and then wheezy -- and just to get started I thought I'd mess around with various programs I've written in the past. Most of these are number-crunching simulations in C/C++.

What I noticed immediately was that some programs took an exceptionally long time to compile -- in some cases so long that I wound up killing the build process, as it was hogging all the system resources. Programs that did compile ran dramatically slower than on my regular laptop -- between 200-300 times slower in fact.

I'm not complaining at all -- it would be insane to expect the setup on the Pi to be able to come anywhere near the performance of an i7 processor -- but I was a little surprised to have quite such a large order of magnitude difference, and I'd like to try and understand why (and if there is any advice on writing Pi-friendly programs).

In the case of compiler speed, the programs that were problematic were fairly template-heavy C++, while C code seemed to compile fairly quickly. So I'm putting that down just to the much more intense effort the compiler has to put in for template-heavy code.

On the other hand, I don't have a ready explanation for the massive speed difference in running programs. At first I thought it might be due to software- rather than hardware-based floating point; a purely integer-based program was relatively faster, but still very slow (about 200 times slower as opposed to nearly 300 times slower for a program with a fair amount of FP). So I'm curious as to why -- and if there are any design choices I can make that might aid performance.

It may be clear by now that my understanding of hardware-related effects on code performance is not great. But that seems a good reason to play with something like the Raspberry Pi -- it's not just kids that can learn from this device ... :-P

Thanks in advance for any thoughts or advice.

Re: Compiler and program speed

Posted: Wed Jun 20, 2012 6:17 pm
by dom
You should use the 224M memory split for compiling: ... mory_split

Raspbian's native compiler will build with hardware fp, so you may be better off using that distribution.

Re: Compiler and program speed

Posted: Wed Jun 20, 2012 7:42 pm
by Janq
You can actually compile with --mfloat-abi=softfp with gcc which will cause it to generate hardware floating point instructions, but still be able to use the software floating-point libraries. Any code inside your program that uses floating point will use hardware floating point, but calling out to any library will use software floating point (eg. if you call sinf(), etc...).

This should work with the compiler in the debian distro (it works with the cross compiler I built under windows anyway).

More on topic: make sure your programs are not causing the OS to swap - that will kill performance. Also: cache size may explain why your programs are 200 times slower (i7 has a huge cache, even L1 is large compared to the Pi's ;)). Even relatively cache-inefficient programs tend to run fast on modern x86, but you might be able to do a lot better by making your code more cache friendly.

Re: Compiler and program speed

Posted: Wed Jun 20, 2012 9:12 pm
by jamesh
Jut a comment on C++ templates - they are extremely resource hungry during compiling., There's a good explanation as to why in Appendix C of C++ Template Metaprogramming by Abrahams and Gurtovoy. It's a good book, but went beyond my intelligence level about half way through.

Re: Compiler and program speed

Posted: Wed Jun 20, 2012 9:34 pm
by johnbeetem
My (standard speed) RasPi has quite good performance, both for compiling my C program and running the resulting executable. It's somewhat slower than my 600 MHz BeagleBoard, but I suspect that may be file access. My RasPi is maybe an order of magnitude slower than my Ubuntu PC. My code makes minimal use of floating point. I do notice a big difference when accessing files -- WebDrake didn't mention how much file access was occurring. I read elsewhere on the RasPi forum that someone has sped up SD card access quite a bit under Debian, but I assume this is not in the general release version.

I've tried running Midori but I've given up as the web sites I'd like to use it with are too slow. This may change with faster SD card access.

All of my speed comparisons are intuitive and from (my) memory, and not any sort of proper scientific measurements. YMMV

Re: Compiler and program speed

Posted: Wed Jun 20, 2012 9:51 pm
by dom
johnbeetem wrote:I've tried running Midori but I've given up as the web sites I'd like to use it with are too slow. This may change with faster SD card access.
Worth trying again with Wheezy image. It is noticably faster.

Re: Compiler and program speed

Posted: Sat Jun 23, 2012 5:38 am
by antiloquax
Try Arch. It's much faster!
Also, if you haven't already done so, you might want to move your file-system off the SD card onto a fast(er) USB drive and use that (you have to alter the command line txt file in the boot partition to use the usb as the root partition).
I have a swap partition on my USB drive (Kingston DataTraveler Elite 3.0), and, although it is still quite slow, it's not unbearable.

Re: Compiler and program speed

Posted: Fri Jun 29, 2012 10:33 pm
by darrenjw
I can't comment on compilation speed, but regarding execution speed, you really need to be using Raspbian, as others have noted. For floating point codes it can make a factor of 10 difference. I've just written a blog post on this topic which you might want to look at: ... aspberry-p