-
Notifications
You must be signed in to change notification settings - Fork 0
/
算法复杂度与时间关系.cpp
66 lines (59 loc) · 1.28 KB
/
算法复杂度与时间关系.cpp
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
65
66
/*
* @Author: Lumen
* @Date: 2021-12-12 14:14:01
* @LastEditTime: 2022-02-27 21:23:31
* @LastEditors: Lumen
* @Description: 算法复杂度与时间的关系
* 👻👻👻👻👻👻👻👻
*/
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
using namespace chrono;
void function1(long long n)
{
long long k = 0;
for (long long i = 0; i < n; i++)
{
k++;
}
}
void function2(long long n)
{
long long k = 0;
for (long long i = 0; i < n; i++)
{
for (long long j = 0; j < n; j++)
{
k++;
}
}
}
void function3(long long n)
{
long long k = 0;
for (long long i = 0; i < n; i++)
{
for (long long j = 1; j < n; j *= 2)
{
k++;
}
}
}
int main()
{
long long n = 0;
while (1)
{
cout << "输入n:";
cin >> n;
milliseconds start_time = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
function1(n);
// function2(n);
// function3(n);
milliseconds end_time = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
cout << "耗时:" << milliseconds(end_time).count() - milliseconds(start_time).count() << " ms" << endl;
}
return 0;
}