title: Measuring performance for sorting data in C++
body: <p>So, our class was given this code to compile and run, and see how long it takes to run for different sized inputs N:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;iterator&gt;
  #include &lt;vector&gt;
  #include &lt;algorithm&gt;
  #include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  
  using namespace std;
  
  int main(int argc, char *argv[]) {
    int N;
    sscanf(argv[1], "%d", &amp;N);
    vector&lt;double&gt; data(N);
    for(unsigned int i=0; i&lt;N; i++) {
      data[i] = rand()/(RAND_MAX+1.0);
    }
    sort(data.begin(), data.end());
    copy(data.begin(), data.end(), ostream_iterator&lt;double&gt;(cout,"\n"));
  }
  </code></pre>
  
  <p>We have never been taught C++ and are not expected to know anything about how this code works. They even give us the commands for compiling and running the code. However, they failed to mention how exactly we can measure how long the program takes. I have tried with this approach:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;iterator&gt;
  #include &lt;vector&gt;
  #include &lt;algorithm&gt;
  #include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;time.h&gt;
  
  using namespace std;
  
  int main(int argc, char *argv[]) {
  
    double start_time = time(NULL);
  
    int N;
    sscanf(argv[1], "%d", &amp;N);
    vector&lt;double&gt; data(N);
    for(unsigned int i=0; i&lt;N; i++) {
      data[i] = rand()/(RAND_MAX+1.0);
    }
    sort(data.begin(), data.end());
    copy(data.begin(), data.end(), ostream_iterator&lt;double&gt;(cout,"\n"));
  
    double end_time = time(NULL);
    printf("%lf seconds\n", end_time - start_time);
  
  }
  </code></pre>
  
  <p>Literally just including a time library, then getting the current time before and after the program runs, and printing the difference at the end.<br>
  All of which I copied straight from this site actually because, again, none of us know (or apparently need to know) how to code anything in C++ until next year.</p>
  
  <p>However, the output is always </p>
  
  <blockquote>
  <pre><code>0.000000 seconds
  </code></pre>
  </blockquote>
  
  <p>even for inputs of sizes in the millions or billions, where I can see that it takes a few seconds or minutes to process.<br>
  What am I doing wrong in this code? </p>
  
  <p>I've read some sources saying to use the Chrono library to measure time but I was getting far more complicated errors when I tried that. This at least compiles and runs, but is just <em>wrong</em> every time.</p>