Re: Returning values from a function

From: Martijn Lievaart (m_at_rtij.nl.removefromhere.invalid)
Date: 10/23/03


Date: Thu, 23 Oct 2003 14:30:56 +0200

On Tue, 21 Oct 2003 11:14:12 -0700, E. Robert Tisdale wrote:

> Premature optimization is a sin.

Absolutely.

> Write code that is easy to read, understand and maintain.

Absolutely.

> Exhaust your compiler's optimization options first.
> Shop around for a better optimizing compiler second.
> Only then should you run your profiler
> and consider cobbling your code to make it run faster.

I don't completely agree here.

First, investigate it. Is it really the program that's causing the delay?
F.I. network protocols without some form of pipelining often are a
bottleneck. I've seen cases where the networkcard was experiencing
congestion becuase of a low mtu. Or is disk I/O the bottleneck? Sometimes
you cannot tell without a profiler, but I found that fairly simple
measurements can give a lot of insight.

I've seen to often that optimization options introduce compiler bugs to
play with them lightly. Also, deviating from the default optimization most
often gives only marginal gains, though for some programs the savings can
be enormous.

An other compiler is often not an option. F.I. opensource often only
compiles on gcc. OTOH, it's an option that is often overlooked. Intel
makes some fairly good compilers that should be drop in replacements for
others (most notably MSVC). A big win if you do numbercrunching.

And before switching compilers, you should profile. More often than not,
some fairly simple changes can make a huge difference. More intelligent
algrithms are the number one efficiency gain, often speeding up a program
by several orders. Switching to non-blocking I/O is another good one
sometimes. I once found through profiling that a program was busy about
20% of the time in converting UTF-8 to and from UCS32. Some simple caching
on this (a 5 minutes patch, isn't encapsulation wonderful) saved that 20%
almost completely.

Just doing a code review can be helpful, sometimes you find that you copy
objects needlessly. Sometimes it helps to break encapsulation to avoid
copies. But first profile to see if this is where the bottlenecks are.

But the number one advice still is measure, measure, measure. Start with
some real world tests and time them. Zoom in on the bottlenecks, some
print statements in the code can be a cheap and effective profiler. Don't
assume, measure. Programmers are notoriously bad at optimizing by
experience, so you need hard facts.

M4



Relevant Pages

  • Re: Returning values from a function
    ... > Shop around for a better optimizing compiler second. ... Or is disk I/O the bottleneck? ... you cannot tell without a profiler, but I found that fairly simple ... I've seen to often that optimization options introduce compiler bugs to ...
    (alt.comp.lang.learn.c-cpp)
  • Re: "Sorting" assignment
    ... too slow, other times for optimizing too much, some times for being too ... That is a rather bizarre, not-quite-C compiler, but ok. ... The overlap issue is a different problem, but performing a "swap" on ... CPU hardware feature differences that can be incredibly important to ...
    (comp.programming)
  • Re: Why INFINITE loop in a thread occupy so much CPU time??
    ... measuring code quality or program efficiency. ... You stated the K&R compiler did the silly thing of testing ... In debug mode, nothing, repeat nothing, matters. ... my Ph.D. is in optimizing compiler technology. ...
    (microsoft.public.vc.mfc)
  • Re: LEN_TRIM performance issue
    ... A long time ago in a Fortran 66 execution profiler ... it) was called FETE, Fortran Execution Time Estimator. ... out to be input to the compiler. ...
    (comp.lang.fortran)
  • Re: Compiler code optimization: see code below
    ... >>I'm writing some C to be used in an embedded environment and the code ... I'm using GCC for the workstation and Diab compiler for the ... >>sure what exactly a good optimizing compiler can optimize away. ... > Neither optimization nor efficiency is defined by the C standard. ...
    (comp.lang.c)