-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy_sample.cpp
More file actions
64 lines (56 loc) · 1.5 KB
/
Copy pathcopy_sample.cpp
File metadata and controls
64 lines (56 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <vector>
#include <algorithm>
#include <sstream>
#include <string>
#include <random>
#include <chrono>
#include <iostream>
#include <ctime>
#include <cstdlib>
int main(int argc, char **argv)
{
// expects two command-line arguments, the data size (an integer) and the number of repetitions (an integer)
std::string arg(argv[1]);
// string stream is used to convert string to int if using C++03 or earlier
std::stringstream ss(arg);
int N;
ss >> N;
int reps;
arg = argv[2];
ss.clear();
ss.str(arg);
ss >> reps;
// create random numbers
// before beginning the timer
unsigned int seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator(seed);
std::uniform_int_distribution<int> distribution(-100 * N, 100 * N);
std::vector<std::vector<int>> nums(reps);
int i, j;
for (j = 0; j < reps; j++)
{
for (i = 0; i < N; i++)
{
int r = distribution(generator);
nums[j].push_back(r);
}
}
// sort numbers
clock_t t1 = clock();
for (i = 0; i < reps; i++)
{
std::sort(nums[i].begin(), nums[i].end());
}
clock_t t2 = clock();
clock_t dt = t2 - t1;
double clocks_per_rep = ((double)dt) / reps;
double seconds = clocks_per_rep / CLOCKS_PER_SEC;
//std::cout << "time: " << dt << " " << seconds << std::endl;
std::cout << N << "," << seconds << std::endl;
// // show we're done
// for(i = 0; i < N; i++)
// {
// std::cout << i << " " << nums[i] << std::endl;
// }
return 0;
}