Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

C/C++

In Defense of Laziness


Saving Resources

Think of yourself as a resource-starved CPU. If you were writing code for that CPU, you'd make sure that you didn't waste cycles or memory [5]. You've seen many of the basic techniques:


char buffer[BUF_SIZE];
void expensive_function()
{
// code using buffer
}

void cheaper_version()
{
static char *buffer = new char[BUF_SIZE];
// code using buffer
}


The second version of this function is cheaper if it's never called: It doesn't tie up memory for a buffer that's never used. [6]


double really_slow_thing(double);
double cached_version(double arg)
{
static double cached_argument;
static double cached_result;
static bool cache_valid = false;
if (!cache_valid || cached_argument != arg)
  {
  cache_valid = true;
  cached_argument = arg;
  cached_result = really_slow_thing(arg);
  }
return cached_result;
}

If you end up calling really_slow_thing with the same value several times in succession and it doesn't have any side effects, caching the result avoids having to recompute the same value, freeing the CPU for other things, at the cost of additional code and data.


double r0 = really_slow_thing(0.0);
double r1 = really_slow_thing(1.0);
double r2 = really_slow_thing(2.0);
double precomputed_version(double arg)
{
return arg == 0.0 ? r0
  : arg == 1.0 ? r1
  : arg == 2.0 ? r2
  : really_slow_thing(arg);
}


When you know in advance that you're going to call really_slow_thing several times with values from some well-defined set, you can speed things up by precomputing the result for each of those values [7]. However, this increases the application's startup time, which could be a problem [8].

Of course, the danger in being lazy is that you might not get the priorities right, and you might not have as much time as you'd like for something that slipped off of your schedule. If you're a resource-starved CPU, this is a disaster. If you're a programmer (or a writer), and you're lucky, you'll have a forgiving boss (or editor) who will let you slide, just this once.

Notes

  1. [1] That's The C++ Standard Library Extensions, coming soon to a bookseller near you.
  2. [2] Okay, I admit it: I, too, debated that sort of thing in my earlier years. And that's important. We need to learn how to write good code, so that the simple loops just roll out of the fingers without much intervention from the brain. Don't get stuck there, though. There really are more important things.
  3. [3] For beginners, this is hopeless. Beginners should not write templates.
  4. [4] Two types do not justify a template, either. Designing for multiple types is far more complex than designing for one or two well-understood types. For example, the Standard library's basic_string template can be instantiated as basic_string<char> and basic_string<wchar_t>. There's an extension mechanism in the char_traits template, which suggests that you can create basic_string instances for other types, but in practice it doesn't work. The change from the original string and wstring classes to the basic_string template was a mistake.
  5. [5] Of course, you should do that anyway, to some extent. But in embedded systems, problems of resource scarcity are more severe than for desktop systems, so resource management is a more prominent part of application design.
  6. [6] On the other hand, if it is used, the initial allocation takes time and uses more memory. Writing and reading data may be slower, too, because the compiler doesn't know the address of the memory at compile time. Sometimes it doesn't pay to be lazy.
  7. [7] This is also the reason that people discuss how to write for loops over vectors: Figuring out the best way beforehand means you don't have to spend time figuring it out while your project's deadline is looming.
  8. [8] Of course, you could lazily evaluate the precomputed values, by initializing r0, r1, and r2 to some known bad value, and computing the actual value when it's first needed. That requires an additional test, unless you're into signaling NaNs.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.