Code one C Instrukcja Użytkownika Strona 2

  • Pobierz
  • Dodaj do moich podręczników
  • Drukuj
  • Strona
    / 4
  • Spis treści
  • BOOKMARKI
  • Oceniono. / 5. Na podstawie oceny klientów
Przeglądanie stron 1
6. Think about instruction-level-parallelism.
Even though many applications still rely on single threaded execution, modern CPUs already have a
significant amount of parallelism inside a single core. This means a single CPU might be simultaneously
executing 4 floating point multiplies, waiting for 4 memory requests, and performing a comparison for an
upcoming branch.
To make the most of this parallelism, blocks of code (i.e., between jumps) need to have enough indepen-
dent instructions to allow the CPU to be fully utilized.
Think about unrolling loops to improve this.
This is also a good reason to use inline functions.
7. Avoid/reduce the number of local variables.
Local variables are normally stored on the stack. However if there are few enough, they can instead be
stored in registers. In this case, the function not only gets the benefit of the faster memory access of data
stored in registers, but the function avoids the overhead of setting up a stack frame.
(Do not, however, switch wholesale to global variables!)
8. Reduce the number of function parameters.
For the same reason as reducing local variables – they are also stored on the stack.
9. Pass structures by reference, not by value.
I know of no case in a ray tracer where structures should be passed by value (even simple ones like Vectors,
Points, and Colors).
10. If you do not need a return value from a function, do not define one.
11. Try to avoid casting where possible.
Integer and floating point instructions often operate on different registers, so a cast requires a copy.
Shorter integer types (char and short) still require the use of a full-sized register, and they need to be padded
to 32/64-bits and then converted back to the smaller size before storing back in memory. (However, this
cost must be weighed against the additional memory cost of a larger data type.)
12. Be very careful when declaring C++ object variables.
Use initialization instead of assignment (Color c(black); is faster than Color c; c = black;).
13. Make default class constructors as lightweight as possible.
Particularly for simple, frequently used classes (e.g., color, vector, point, etc.) that are manipulated fre-
quently.
These default constructors are often called behind your back, where you are not expecting it.
Use constructor initializer lists. (Use Color::Color() : r(0), g(0), b(0) {} rather than Color::Color() { r
= g = b = 0; } .)
14. Use shift operations >> and << instead of integer multiplication and division, where possible.
15. Be careful using table-lookup functions.
Many people encourage using tables of precomputed values for complex functions (e.g., trigonometric
functions). For ray tracing, this is often unnecessary. Memory lookups are exceedingly (and increasingly)
expensive, and it is often as fast to recompute a trigonometric function as it is to retrieve the value from
memory (especially when you consider the trig lookup pollutes the CPU cache).
Przeglądanie stron 1
1 2 3 4

Komentarze do niniejszej Instrukcji

Brak uwag