Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- VtArray<float> value;
- // This will be a very very very slow loop.
- // The non-const array accessor constantly checks to see if the
- // data is not-unique (i.e. is being used in more than one place).
- // If it is not unique, then the data will be copied.
- for(size_t i = 0; i < 100; ++i)
- {
- value[i] = float(i);
- }
- // However, if you used range-based for loops, then that check is performed once
- // within the call to begin (rather than each call to []), and the loop body
- // will be vectorised by the compiler.
- size_t i = 0;
- for(auto& f : value)
- {
- f = float(i++);
- }
- // If you need to use the array brackets, then prefer taking a temp copy of
- // data() and using that instead. Again, this should vectorise.
- float* const ptemp = value.data();
- for(size_t i = 0; i < 100; ++i)
- {
- ptemp[i] = float(i);
- }
- // Where possible use const& (Which doesn't perform the unique check at all)
- const VtArray<float>& cvalue = value;
- // or make use of cdata() rather than data()
- auto ptr = cvalue.cdata();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement