Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. VtArray<float> value;
  2.  
  3. // This will be a very very very slow loop.
  4. // The non-const array accessor constantly checks to see if the
  5. // data is not-unique (i.e. is being used in more than one place).
  6. // If it is not unique, then the data will be copied.
  7. for(size_t i = 0; i < 100; ++i)
  8. {
  9. value[i] = float(i);
  10. }
  11.  
  12. // However, if you used range-based for loops, then that check is performed once
  13. // within the call to begin (rather than each call to []), and the loop body
  14. // will be vectorised by the compiler.
  15. size_t i = 0;
  16. for(auto& f : value)
  17. {
  18. f = float(i++);
  19. }
  20.  
  21. // If you need to use the array brackets, then prefer taking a temp copy of
  22. // data() and using that instead. Again, this should vectorise.
  23. float* const ptemp = value.data();
  24. for(size_t i = 0; i < 100; ++i)
  25. {
  26. ptemp[i] = float(i);
  27. }
  28.  
  29. // Where possible use const& (Which doesn't perform the unique check at all)
  30. const VtArray<float>& cvalue = value;
  31.  
  32. // or make use of cdata() rather than data()
  33. auto ptr = cvalue.cdata();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement