title: What is the fastest way to generate a sequence from n to m with large numbers?
body: <p>I'm creating a program where speed counts. I use "default" large numbers (with GMP), so that it is easier to handle for large calculations, when necessary (so I don't have an <code>int</code>, <code>long</code> or other).</p>
  
  <p>For a feature, I should generate an array containing positive numbers from n to m (e.g. all numbers from 0 to 10). Sometimes I need to generate several million numbers. So I wonder what is the fastest way to fill an array from n to m with GMP numbers (<code>mpz_t</code>), knowing that <code>mpz_array_init</code> <a href="https://gmplib.org/manual/Integer-Special-Functions.html#Integer-Special-Functions" rel="nofollow noreferrer">is obsolete</a>.</p>
  
  <p>At the moment I have this:</p>
  
  <pre><code>void generate_seq(mpz_t *res_ptr, const size_t from, const size_t until)
  {
      const size_t len = until - from;
      if (res_ptr == NULL)
          res_ptr = malloc(len * sizeof *res_ptr);
      size_t curr = len;
      while (curr-- &gt; from)
          mpz_init_set_ui(res_ptr[curr], curr);
  }
  </code></pre>
  
  <p>And I test with this:</p>
  
  <pre><code>mpz_t *array = NULL;
  generate_seq(array, 0, (size_t)1e8);
  </code></pre>
  
  <p>Is there a faster way? Maybe by using vectorization (I am not familiar with SIMD)?</p>
  
  <p>PS: It's not really a bottleneck, but it's more out of curiosity that I ask this question.</p>