Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Is there a better way to do this?</p> </blockquote> <p>Yup, there are a few things you might want to do here to help speed things up. As mentioned before, reserving space in the std::vector and then assigning values to the known elements, is faster. Also, pre incrementing ( ++var instead of var++ ) is faster when using non optimized compilers. Just for the sake of keeping your code fast, no matter who builds it, you might want to consider doing that from now on. As far as memory goes, you may find it trivial, but when I use known sizes that are unsigned, and not unreasonably large, I use unsigned short's for my for loops.</p> <p>About the modulo, however. You might want to not use it, if you don't need it. Depending on the data type held in the vector, your results should wrap if they go above the maximum storage capacity of the type.</p> <p>I don't know off hand if it eats up more processing power having variables wrap, and if it does, I'm still not sure if its a less expensive operation then preforming modulo. Might want to run some speed tests with known sizes before going with rand. </p> <pre><code> A.reserve(i * i); for(unsigned short j = 0; j &lt; 10; ++j) { for(unsigned short k = 0; k &lt; i; ++k) A[k + (i*10)] = rand(); // Other stuff } </code></pre> <p><strong>Edit</strong></p> <p>Very small change to note: The loop is going only 10 times, so you might as well use an unsigned char, rather than a short. On Win32 at the very least, it takes half the memory.</p> <pre><code> A.reserve(i * i); for(unsigned char j = 0; j &lt; 10; ++j) { for(unsigned char k = 0; k &lt; i; ++k) A[k + (i*10)] = rand(); // Other stuff } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload