Optimizing Software In C++ Language For Windows, Linux and Mac Platforms

optimizing software in cpp
Click Here To Download Book
Pages 164
Views 7622
Size 1.7 MB
Download 48

Table Of Contents1


The costs of optimizing4

Choosing the optimal platform5

Choice of hardware platform5

Choice of microprocessor6

Choice of operating system6

Choice of programming language7

Choice of compiler10

Choice of function libraries12

Choice of user interface framework14

Overcoming the drawbacks of the C++ language14

Finding the biggest time consumers16

How much is a clock cycle?16

Use a profiler to find hot spots16

Program installation18

Automatic updates19

Program loading19

Dynamic linking and position-independent code20

File access20

System database20

Other databases21


Other system resources21

Network access21

Memory access21

Context switches22

Dependency chains22

Execution unit throughput22

Performance and usability23

Choosing the optimal algorithm24

Development process25

The efficiency of different C++ constructs26

Different kinds of variable storage26

Integers variables and operators29

Floating point variables and operators32



Pointers and references36

Function pointers37

Member pointers37

Smart pointers38


Type conversions40

Branches and switch statements43



Function parameters50

Function return types50

Structures and classes51

Class data members (properties)51

Class member functions (methods)53

Virtual member functions53

Runtime type identification (RTTI)54


Constructors and destructors55



Overloaded functions56

Overloaded operators56



Exceptions and error handling61

Other cases of stack unwinding65

Preprocessing directives65


Optimizations in the compiler66

How compilers optimize66

Comparison of different compilers74

Obstacles to optimization by compiler77

Obstacles to optimization by CPU81

Compiler optimization options81

Optimization directives82

Checking what the compiler does84

Optimizing memory access87

Caching of code and data87

Cache organization87

Functions that are used together should be stored together88

Variables that are used together should be stored together88

Alignment of data90

Dynamic memory allocation90

Container classes93


Access data sequentially96

Cache contentions in large data structures96

Explicit cache control99



Out of order execution103

Using vector operations105

AVX instruction set and YMM registers107

AVX-512 instruction set and ZMM registers107

Automatic vectorization107

Using intrinsic functions109

Using vector classes113

Transforming serial code for vectorization117

Mathematical functions for vectors119

Aligning dynamically allocated memory120

Aligning RGB video or 3-dimensional vectors120


Making critical code in multiple versions for different instruction sets122

CPU dispatch strategies122

Model-specific dispatching124

Difficult cases124

Test and maintenance126


CPU dispatching in Gnu compiler128

CPU dispatching in Intel compiler130

Specific optimization topics132

Use lookup tables132

Bounds checking134

Use bitwise operators for checking multiple values at once135

Integer multiplication136

Integer division137

Floating point division139

Don’t mix float and double140

Conversions between floating point numbers and integers141

Using integer operations for manipulating floating point variables142

Mathematical functions145

Static versus dynamic libraries146

Position-independent code148

System programming150


Testing speed153

Using performance monitor counters155

The pitfalls of unit-testing156

Worst-case testing157

Optimization in embedded systems158

Overview of compiler options160


Copyright notice164

The I/O Manipulators187

Checking for Errors188

Opening and Closing a File289

Write Formatted Data to a Text File293

Options and Alternatives296

Read Formatted Data from a Text File296

Options and Alternatives300

Write Unformatted Binary Data to a File300

Options and Alternatives304

Read Unformatted Binary Data from a File305

Options and Alternatives309

Use get( )and getline( )to Read from a File310

Options and Alternatives313

Read from and Write to a File314

Options and Alternatives317

Detecting EOF317

Bonus Example: A Simple File-Comparison Utility320

Options and Alternatives322

Use Exceptions to Detect and Handle I/O Errors322

Options and Alternatives326

Use Random-Access File I/O326

Bonus Example: Use Random-Access I/O to Access Fixed-Size Records329

Options and Alternatives332

Look Ahead in a File332

Options and Alternatives336

Use the String Streams337

Options and Alternatives340

Create Custom Inserters and Extractors341

Options and Alternatives344

Create a Parameterless Manipulator344

Options and Alternatives347

Create a Parameterized Manipulator348

Options and Alternatives352

Obtain or Set a Stream’s Locale352

Options and Alternatives355

Use the C-Based File System355

Options and Alternatives361

Rename and Remove a File363

Options and Alternatives365

Formatting Data367

Formatting Overview368

The Format Flags368

The Field Width, Precision, and Fill Character369

Format-Related Stream Member Functions370

The I/O Manipulators370

Format Data Using the Localization Library370

The printf( )Family of Functions371

The strftime( )Function371

Access the Format Flags via Stream Member Functions374

Bonus Example: Display the Format Flag Settings376

Options and Alternatives378

Display Numeric Values in Various Formats379

Options and Alternatives382

Set the Precision383

Options and Alternatives384

Set the Field Width and Fill Character385

Bonus Example: Line Up Columns of Numbers387

Options and Alternatives388

Justify Output388

Options and Alternatives391

Use I/O Manipulators to Format Data391

Options and Alternatives395

Format Numeric Values for a Locale395

Options and Alternatives397

Format Monetary Values Using the money_putFacet398

Options and Alternatives401

Use the moneypunctand numpunctFacets402

Options and Alternatives405

Format Time and Date Using the time_putFacet407

Options and Alternatives411

Format Data into a String412

Options and Alternatives414

Format Time and Date Using strftime( )414

Options and Alternatives417

Use printf( )to Format Data418

Options and Alternatives424


Operator Overloading Basic Techniques426

Options and Alternatives435

Overload the Function Call Operator ( )437

Options and Alternatives440

Overload the Subscripting Operator [ ]441

Options and Alternatives445

Overload the –>Operator445

Bonus Example: A Simple Safe Pointer Class447

Options and Alternatives451

Overload newand delete451

Options and Alternatives456

Overload the Increment and Decrement Operators457

Options and Alternatives462

Create a Conversion Function463

Options and Alternatives466

Create a Copy Constructor466

Bonus Example: A Safe Array that Uses Dynamic Allocation471

Options and Alternatives477

Determine an Object’s Type at Runtime478

Options and Alternatives484

Use Complex Numbers484

Options and Alternatives487

Use auto_ptr487

Options and Alternatives490

Create an Explicit Constructor491

Options and Alternatives494