-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstaticvheap.cpp
More file actions
47 lines (45 loc) · 1.18 KB
/
staticvheap.cpp
File metadata and controls
47 lines (45 loc) · 1.18 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
#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
const int AMT = 60000000;
void f0() {
int* v = (int*) calloc(AMT, sizeof(int));
cout<<"Random El: "<<v[rand()%AMT]<<endl;
}
void f1() {
vector<int> v(AMT);
cout<<"Random El: "<<v[rand()%AMT]<<endl;
}
void f2() {
if (AMT > 1e5) {
cout<<"Too Large For Stack"<<endl;
} else {
int v[AMT] = { 0 };
cout<<"Random El: "<<v[rand()%AMT]<<endl;
}
}
void f3() {
static int v[AMT] = { 0 };
cout<<"Random El: "<<v[rand()%AMT]<<endl;
}
void f4() {
int *v = new int[AMT];
cout<<"Random El: "<<v[rand()%AMT]<<endl;
}
void f5() {
static vector<int> v(AMT);
cout<<"Random El: "<<v[rand()%AMT]<<endl;
}
int main() {
srand(time(NULL)); // Seed the random number generator
// Create an array of function pointers
void (*functions[])() = {f0, f1, f2, f3, f4, f5};
for (int i = 0; i < sizeof(functions) / sizeof(functions[0]); ++i) {
auto start = chrono::high_resolution_clock::now();
functions[i]();
auto end = chrono::high_resolution_clock::now();
chrono::duration<double> elapsed = end - start;
cout << "Time taken by f" << i << "(): " << elapsed.count() << " seconds" << endl;
}
return 0;
}