Title: Measuring performance for sorting data in C++
Tags: c++
Question: So, our class was given this code to compile and run, and see how long it takes to run for different sized inputs N:
#include <iostream&gt;
#include <fstream&gt;
#include <iterator&gt;
#include <vector&gt;
#include <algorithm&gt;
#include <iostream&gt;
#include <stdio.h&gt;
using namespace std;
int main(int argc, char *argv[]) {
int N;
sscanf(argv[1], "%d", &amp;N);
vector<double&gt; data(N);
for(unsigned int i=0; i<N; i++) {
data[i] = rand()/(RAND_MAX+1.0);
sort(data.begin(), data.end());
copy(data.begin(), data.end(), ostream_iterator<double&gt;(cout,"\n"));
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:
#include <iostream&gt;
#include <fstream&gt;
#include <iterator&gt;
#include <vector&gt;
#include <algorithm&gt;
#include <iostream&gt;
#include <stdio.h&gt;
#include <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<double&gt; data(N);
for(unsigned int i=0; i<N; i++) {
data[i] = rand()/(RAND_MAX+1.0);
sort(data.begin(), data.end());
copy(data.begin(), data.end(), ostream_iterator<double&gt;(cout,"\n"));
double end_time = time(NULL);
printf("%lf seconds\n", end_time - start_time);
Literally just including a time library, then getting the current time before and after the program runs, and printing the difference at the end.
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.
However, the output is always
0.000000 seconds
even for inputs of sizes in the millions or billions, where I can see that it takes a few seconds or minutes to process.
What am I doing wrong in this code?
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 wrong every time.
Comment: Try `auto start_time = time(NULL)` and `auto end_time = time(NULL)` instead. When the values get large, `double`  may not be precise enough to differentiate them.

Comment: See the [``](https://en.cppreference.com/w/cpp/chrono) header for more modern timing features.

Comment: @SidS Thanks, wasn't thinking straight.


Here is another answer: The time function returns a second count (an integral count, not a floating point number). It seems that the algorithm does not run long enough and you end up with an execution time of less than a second. The easiest way to make the execution time measurable is to introduce an outer loop like so:
for (int n=0; n<NumLoops; n++)
// your logic
with NumLoops adjusted to a large number (start low-ish and increase) until your execution time is maybe 30 seconds or longer. Then divide the total time measured by NumLoops.
Note that this is not a high precision approach. It does not measure actual thread execution time (that's another subject) and assumes that the execution time of the inner logic far outweighs the overheads introduced by the outer loop (which seems to be the case here).

Here is another answer: You were probably expected to use common tools of the environment, rather than modifying the code.
For example, in Linux, the time tool:
g++ theCode.cpp -o theProgram
time ./theProgram 10
time ./theProgram 100
time ./theProgram 1000
time ./theProgram 10000
time(NULL) doesn't return a double; it returns a time_t. You've converted both timestamps to double and possibly caused yourself precision problems, since UNIX timestamps are quite high and the number of seconds' difference you're expecting is relatively small.
You should get rid of the doubles and stick with the time_t type that time(NULL) gives you. Don't forget to update your printf format string from %lff to something else.
Also, it's better spelt time(nullptr) now, or you could use the modern C++ features in <chrono&gt;.
Comment for this answer: Addendum: The magic that allows `printf` and its large family to accept an undefined number of parameters of undefined types makes it impossible for the compiler to verify that the parameters provided make sense. The function can and will take any given parameters no matter how hilariously wrong. Some compilers have added extensions to inspect the format string and warn about mismatches, so check the build output even if the program compiled. It could save you much debugging.
Comment for this answer: @Bill Are you on Linux?
Comment for this answer: Using `time ./theProgram` just returns the error `The system cannot accept the time entered`.
I also tried changing the `double` to `time_t` and it now displays exactly `4294967296 seconds` every time, regardless of input size.

Here is another answer: This is likely because the time that had elapsed was less than a second.  The resolution of time() is in seconds.
Here's how to switch of use the chrono library to measure in nanoseconds:
#include <iostream&gt;
#include <fstream&gt;
#include <iterator&gt;
#include <vector&gt;
#include <algorithm&gt;
#include <iostream&gt;
#include <stdio.h&gt;
#include <time.h&gt;
#include <chrono&gt;
using namespace std;
using namespace st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0chrono;
int main(int argc, char *argv[]) {
high_resolution_clock75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0time_point start_time = high_resolution_clock75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0now();
int N;
sscanf(argv[1], "%d", &amp;N);
vector<double&gt; data(N);
for(unsigned int i=0; i<N; i++) {
data[i] = rand()/(RAND_MAX+1.0);
sort(data.begin(), data.end());
copy(data.begin(), data.end(), ostream_iterator<double&gt;(cout,"\n"));
high_resolution_clock75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0time_point end_time = high_resolution_clock75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0now();
printf("%lf nanoseconds\n", duration_cast< nanoseconds &gt;( end_time - start_time ).count() );
Comment for this answer: Any time I try to use
`#include `
I get the error:
`This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options`
I looked a little into compiler options, it definitely seems way beyond my understanding, though I did try typing `-std=c++11` in a few places, all of which just gave more various errors.
Comment for this answer: @Bill, the following should work `g++ -std=c++11 -o app source.cpp`.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:02/21/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c++>', 'Title': 'Measuring performance for sorting data in C++', 'ViewCount': 96, 'id': 54814101, 'user_AboutMe': None, 'user_AccountId': 12289470.0, 'user_DisplayName': 'Bill', 'user_Reputation': 41.0, 'user_Views': 10.0, 'user_id': 8967447.0}}