Code one C Manual de usuario Pagina 3

  • Descarga
  • Añadir a mis manuales
  • Imprimir
  • Pagina
    / 4
  • Tabla de contenidos
  • MARCADORES
  • Valorado. / 5. Basado en revisión del cliente
Vista de pagina 2
In other instances, lookup tables may be quite useful. For GPU programming, table lookups are often
preferred for complex functions.
16. For most classes, use the operators += , -= , *= , and /= , instead of the operators + , - , * , and / .
The simple operations need to create an unnamed, temporary intermediate object.
For instance: Vector v = Vector(1,0,0) + Vector(0,1,0) + Vector(0,0,1); creates ve unnamed, temporary
Vectors: Vector(1,0,0), Vector(0,1,0), Vector(0,0,1), Vector(1,0,0) + Vector(0,1,0), and Vector(1,0,0) +
Vector(0,1,0) + Vector(0,0,1).
The slightly more verbose code: Vector v(1,0,0); v+= Vector(0,1,0); v+= Vector(0,0,1); only creates two
temporary Vectors: Vector(0,1,0) and Vector(0,0,1). This saves 6 functions calls (3 constructors and 3
destructors).
17. For basic data types, use the operators + , - , * , and / instead of the operators += , -= , *= , and /= .
18. Delay declaring local variables.
Declaring object variable always involves a function call (to the constructor).
If a variable is only needed sometimes (e.g., inside an if statement) only declare when necessary, so the
constructor is only called if the variable will be used.
19. For objects, use the prefix operator (++obj) instead of the postfix operator (obj++).
This probably will not be an issue in your ray tracer.
A copy of the object must be made with the postfix operator (which thus involves an extra call the the
constructor and destructor), whereas the prefix operator does not need a temporary copy.
20. Be careful using templates.
Optimizations for various instantiations may need to be different!
The standard template library is reasonably well optimized, but I would avoid using it if you plan to
implement an interactive ray tracer.
Why? By implementing it yourself, you’ll know the algorithms it uses, so you will know the most efficient
way to use the code.
More importantly, my experience is that debug compiles of STL libraries are slow. Normally this isn’t a
problem, except you will be using debug versions for profiling. You’ll find STL constructors, iterators,
etc. use 15+% of your run time, which can make reading the profile output more confusing.
21. Avoid dynamic memory allocation during computation.
Dynamic memory is great for storing the scene and other data that does not change during computation.
However, on many (most) systems dynamic memory allocation requires the use of locks to control a
access to the allocator. For multi-threaded applications that use dynamic memory, you may actually get a
slowdown by adding additional processors, due to the wait to allocate and free memory!
Even for single threaded applications, allocating memory on the heap is more expensive than adding it
on the stack. The operating system needs to perform some computation to find a memory block of the
requisite size.
22. Find and utilize information about your system’s memory cache.
If a data structure fits in a single cache line, only a single fetch from main memory is required to process
the entire class.
Vista de pagina 2
1 2 3 4

Comentarios a estos manuales

Sin comentarios