title: Why is my Rust program that performs array reversal slower than the equivalent Go program?
body: <p>I am initialising an array and then reversing it a number of times to see the performance.</p>
  
  <p>I want to understand if I have written code which cannot be compared or is Rust really so bad that it is taking so much time?</p>
  
  <p>Here is the build and timing process of Rust:</p>
  
  <pre class="lang-none prettyprint-override"><code>rustc main.rs
  time ./main
  </code></pre>
  
  <p>This runs and goes on and on. Which is surprising</p>
  
  <h3>Rust</h3>
  
  <pre><code>fn reverse(mylist: &amp;mut Vec&lt;u16&gt;) {
      let length = mylist.len();
  
      let mid_length = length / 2;
  
      for number in 0..mid_length {
          let mut a = mylist[number];
          let mut b = mylist[length - number - 1];
  
          mylist[number] = b;
  
          mylist[length - number - 1] = a;
      }
  }
  
  fn main() {
      let array_size = 100000;
  
      let iterations = 100000;
  
      let mut v = vec![0u16; array_size];
  
      for _ in 0..iterations {
          reverse(&amp;mut v);
      }
  }
  </code></pre>
  
  <h3>Go</h3>
  
  <p>The Go code does exactly what Rust code is doing above. The important thing to note is that Go has Garbage Collection where as Rust does not. Surprisingly, Go does the job in less than 6 seconds:</p>
  
  <pre class="lang-none prettyprint-override"><code>go build main.go
  time ./main 100000 100000
  
  real    0m5.932s
  user    0m5.928s
  sys 0m0.004s
  </code></pre>
  
  <h3>Go</h3>
  
  <pre class="lang-golang prettyprint-override"><code>package main
  
  import (
      "os"
      "strconv"
  )
  
  func reverse(mylist []int) []int {
  
      length := len(mylist)
      half := int(length / 2)
  
      for i := 0; i &lt; half; i++ {
  
          mylist[i], mylist[length-i-1] = mylist[length-i-1], mylist[i]
  
      }   
  
      return mylist
  
  }
  
  func main() {
  
      array_size, _ := strconv.Atoi(os.Args[1])
      iterations, _ := strconv.Atoi(os.Args[2])
  
      mylist := make([]int, array_size)
  
      for i := 0; i &lt; iterations; i++ {
          reverse(mylist)
  
      }
  
  }
  </code></pre>