使用常量 变量 时间上的区别

以前感觉使用变量比使用常量慢,因为要获得变量的值,

其实不是这样的,

变量和常量都是存储在地址空间,

需要的时候拿出来用,时间上没差别。(我是这样认为的,无法保证正确)

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <string>
 6 #include <algorithm>
 7 #include <set>
 8 #include <map>
 9 #include <queue>
10 #include <iostream>
11 using namespace std;
12 
13 #define ll long long
14 
15 const int maxn=1e4+10;
16 const int inf=1e9;
17 const double eps=1e-8;
18 
19 
20 
21 int main()
22 {
23     int i,j,a=10,b=20;
24     for (j=1;j<=1000;j++)
25         for (i=1;i<=10000000;i++)
26 //            if (a>b)
27             if (10>20)
28                 printf("ok\n");
29     return 0;
30 }
31 /*
32 10>20   15.066 s
33         14.791 s
34 a>b     15.318 s
35         14.697 s
36 */

猜你喜欢

转载自www.cnblogs.com/cmyg/p/10810199.html
今日推荐