All My Brain Where stuff from my brain lands

August 9, 2008

How much overhead does C++ bring compared to straight C?

Filed under: Programming — Tags: , , — Dennis @ 2:39 pm

The other day, I had a conversation about putting C code onto an embedded chip. I wondered if it was possible to put C++ code on there. The gist of the conversation was that the C++ libraries had a lot of overhead and the executable size would be too large for the space available on the chip. I’m not an expert on exactly how much space is available on such devices (although I plan on becoming one), but I thought I’d play around with a few things and see just what overhead a c++ program would have when compared with it’s C counterparts.

I should note that there is already comparisons of the actual language constructs of C++ vs C for embedded devices. A simple google search yielded me a few answers. I’m more interested here in library overhead. For example, how does printf compare to cout, etc. All my experiments are done on my iMac with gcc/g++.

(more…)

August 6, 2008

3 Optimizations for speeding Visual C++ compiled code.

Filed under: Programming — Tags: , , , , — Dennis @ 7:55 am

For fun, I participated in a programming contest. Instead of describing the process this time, I thought I’d include a function. Can you guess what it does? I’m changing some of the variable names so it isn’t obvious.

unsigned short result [dim][dim];
void foo(const string& info1, const string& info2, string& ret_val) {
 
 for (int x=0;x=0;--x) {
  
  use_high=x=0);
  sum += carry;
  carry = sum / 10;
  ret_val.insert( ret_val.begin(), (char)(sum % 10 + '0') );
 }
}

Anyhow, now that that fun is over, I was reading comments on a C++ forum and someone mentioned a few tips to optimizing C++ code with Visual C++. I thought I’d do a quick comparison between these and see how much of a difference it made since I was interested in program execution time and already had timing code built into my program.

  1. Release build

    I was already doing this, but I thought I’d include it for the sake of being complete.

    Debug build running time: 0.09 seconds
    Release Build running time: .02 seconds

    Obviously, this is a big one.

  2. Optimizations

    Optimizations are usually the biggest difference between release and debug builds. By default, my compiler was adding /O2 to build for maximum speed. I decided I’d enable every other optimization possible

    Release build optimizations settings

    Release build optimizations settings

    Anyway, the funny thing was, it ran faster by leaving the settings as they were. Obviously, you’ll need to test these and know what they do before simply enabling them. One thing that did seem to make a difference though, was to link against the Multi-threaded library instead of the Multi-threaded DLL. (/MT instead of /MD)

  3. Disable secure stl

    I found references to adding /D_SECURE_SCL=0 to the compiler command line. I assume if you heavily use iterators with STL, this might make more of a difference. For my little program, it didn’t seem to do much at all. I’m not completely sure it was even working. I’m assuming for now though, that it will do something, but that my program isn’t a good test case.

Powered by WordPress

css.php
%d bloggers like this: