WebDrake
Posts: 1
Joined: Wed Jun 20, 2012 10:06 am

Compiler and program speed

Wed Jun 20, 2012 5:51 pm

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.

dom
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5370
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge

Re: Compiler and program speed

Wed Jun 20, 2012 6:17 pm

You should use the 224M memory split for compiling:
http://elinux.org/R-Pi_Troubleshooting# ... mory_split

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

Janq
Posts: 36
Joined: Sat Jun 02, 2012 3:36 pm

Re: Compiler and program speed

Wed Jun 20, 2012 7:42 pm

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.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 24129
Joined: Sat Jul 30, 2011 7:41 pm

Re: Compiler and program speed

Wed Jun 20, 2012 9:12 pm

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.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“I think it’s wrong that only one company makes the game Monopoly.” – Steven Wright

User avatar
johnbeetem
Posts: 945
Joined: Mon Oct 17, 2011 11:18 pm
Location: The Mountains
Contact: Website

Re: Compiler and program speed

Wed Jun 20, 2012 9:34 pm

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

dom
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5370
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge

Re: Compiler and program speed

Wed Jun 20, 2012 9:51 pm

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.

antiloquax
Posts: 406
Joined: Sun Nov 20, 2011 11:37 am
Contact: Website

Re: Compiler and program speed

Sat Jun 23, 2012 5:38 am

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.
mark

darrenjw
Posts: 3
Joined: Fri Jun 29, 2012 10:23 pm

Re: Compiler and program speed

Fri Jun 29, 2012 10:33 pm

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: http://darrenjw.posterous.com/floating- ... aspberry-p

Return to “C/C++”