Articles & Books

Common Optimizations -- Andrzej Krzemieński

Today from the desk of Andrzej:

Common Optimizations

by Andrzej Krzemieński

From the article:

Language designers, compiler and library vendors make a great effort to make your programs run faster and faster. This post is a tour of some common performance optimizations in C++.

Consider the following code that deals with std::string: ...

Parsing XML at the Speed of Light--Arseny Kapoulkine

Some high-performance techniques that you an use for more than just parsing, including this week's darling of memory management:

Parsing XML at the Speed of Light

a chapter from "The Performance of Open Source Applications"
by Arseny Kapoulkine

From the chapter:

This chapter describes various performance tricks that allowed the author to write a very high-performing parser in C++: pugixml. While the techniques were used for an XML parser, most of them can be applied to parsers of other formats or even unrelated software (e.g., memory management algorithms are widely applicable beyond parsers). ...

Optimizing software is hard. In order to be successful, optimization efforts almost always involve a combination of low-level micro-optimizations, high-level performance-oriented design decisions, careful algorithm selection and tuning, balancing among memory, performance, implementation complexity, and more. Pugixml is an example of a library that needs all of these approaches to deliver a very fast production-ready XML parser–even though compromises had to be made to achieve this. A lot of the implementation details can be adapted to different projects and tasks, be it another parsing library or something else entirely.

Continue reading...

What does [this] mean in C++? -- StackOverflow

Recently on SO:

What does "[ this ]" mean in C++?

When I was reading the Cocos2dx 3.0 API, I found something like this:

auto listener = [this](Event* event){
    auto keyboardEvent = static_cast<EventKeyboard*>(event);
    if (keyboardEvent->_isPressed)
    {
        if (onKeyPressed != nullptr)
            onKeyPressed(keyboardEvent->_keyCode, event);
    }
    else
    {
        if (onKeyReleased != nullptr)
            onKeyReleased(keyboardEvent->_keyCode, event);
    }
};

What does [this] mean? Is this new syntax in C++11?

Overload 120 is now available

overload-120.PNGOverload 120 is now available. It contains the following C++-related articles, and more:

 

Overload 120

Enforcing the Rule of Zero

Juan Alday considers how the new standards have affected a common rule.

Search with CppCheck

Martin Moene tries a more powerful code search tool.

Size Matters

 

Sergey Ignatchenko and Dmytro Ivanchykhin compare 32-bit and 64-bit programs.

Windows 64-bit Calling Conventions

Roger Orr sees how the stack organisation has changed.

Are contiguous C++ arrays really faster than Java/C# ArrayLists? -- StackOverflow

When you see anyone claim performance parity between <other language> and C++, one of the first things to look for is whether the C++ version of their test code is correctly using arrays and traversing them in order. If the test code is just doing equivalent pointer-chasing in both languages, the performance comparison is largely meaningless because the program is probably memory-bound and not properly written to use C++'s default container (vector).

Note: The question below has been modified since originally posted and now shows more reasonable numbers that demonstrate contiguous arrays are indeed faster. The comments are still enlightening to read, however.

Today on SO:

Unable to reproduce: C++ Vector performance advantages over C# List performance

At Microsoft's BUILD conference Herb Sutter explained that C++ has "Real Arrays" and C#/Java languages do not have the same or sort of.

I was sold on that. You can watch the full talk here http://channel9.msdn.com/Events/Build/2014/2-661

Here is a quick snapshot of the slide where he described this. http://i.stack.imgur.com/DQaiF.png

But I wanted to see how much difference will I make.

So I wrote very naive programs for testing, [...] Here are the results on my dell laptop with Core i7 processor:

count       C# (List<string>)   C# (ArrayList)     C++  
1000           24 ms              21 ms             7 ms      
10000         214 ms             213 ms            64 ms    
100000  2 sec 123 ms       2 sec 125 ms           678 ms

Declare functions noexcept wherever possible? -- Scott Meyers

Scott Meyers' work on his new "Effective C++" book, tentatively titled Effective Modern C++, progresses with an updated draft item:

Declare functions noexcept wherever possible?

by Scott Meyers

From the article:

In the comments following my last post, there was some controversy regarding the wording of my advice on noexcept functions. My advice is "Declare functions noexcept whenever possible." Some people appear to be concerned that this could be misconstrued as advocating noexcept even when it makes no sense, but I think the advice is a reasonable conclusion to the Item that supports it. I posted a draft version of that Item in early February, but I've revised the draft since then, and I'm making the current draft available now: ...









Control Structures in C++ -- Prashant Sharma

[For very new programmers, this is a basic review of the control flow language features available in C++ -- most of them also valid C, but at our request now with coverage also of the C++-specific range-for loop. -- Ed.]

Now on life`n`gadget:

Control Structures in C++ (Flow of Control)

by Prashant Sharma

From the article contents:

1 Control Structures in C++

1.1 Introduction

1.2 Selection Structure (Branching Statements)

1.2.1 if Statement

1.2.2 if-else Statement

1.2.3 Nested if else Statement

1.2.4  switch Statement

1.2.4.1 switch vs if-else

1.3 Looping Structure(Iterative Statements)

1.3.1 Elements of Looping Structure

1.3.2  for-loop

1.3.3 Range-for statement (range based for-loop) New C++11 Feature

1.3.4 while-loop

1.3.5 do-while loop

1.3.6 Related