由于min跟max是等价的,下面只对求min进行测试
每次测试执行min 109次,记录时间(单位:秒)
耗时以STL为参考基准
运行环境
Windows 7 32-bit on Intel Pentium G2030 @ 3.0GHz
编译器
MinGW GCC 4.8.1 32-bit Release
附加命令
-g -Wall -Wl,-stack,100000000
int 单变量
测试次数 |
Test 1 |
Test 2 |
Test 3 |
Avg |
百分比 |
for循环 |
2.121 |
2.106 |
2.090 |
2.106 |
56.2% |
STL |
3.744 |
3.744 |
3.744 |
3.744 |
100.0% |
普通函数 |
3.386 |
3.385 |
3.401 |
3.391 |
90.6% |
inline普通函数 |
3.432 |
3.448 |
3.401 |
3.427 |
91.5% |
传引用函数 |
3.229 |
3.073 |
3.058 |
3.120 |
83.3% |
inline传引用函数 |
3.447 |
3.401 |
3.400 |
3.416 |
91.2% |
define |
2.387 |
2.387 |
2.403 |
2.392 |
63.9% |
int 表达式
测试次数 |
Test 1 |
Test 2 |
Test 3 |
Avg |
百分比 |
for循环 |
2.091 |
2.090 |
2.106 |
2.096 |
30.0% |
STL |
7.004 |
6.973 |
6.973 |
6.983 |
100.0% |
普通函数 |
6.178 |
6.178 |
6.178 |
6.178 |
88.5% |
inline普通函数 |
6.162 |
6.162 |
6.162 |
6.162 |
88.2% |
define |
5.273 |
5.288 |
5.289 |
5.283 |
75.7% |
long long 单变量
测试次数 |
Test 1 |
Test 2 |
Test 3 |
Avg |
百分比 |
for循环 |
2.169 |
2.090 |
2.106 |
2.122 |
51.6% |
STL |
4.118 |
4.103 |
4.103 |
4.108 |
100.0% |
普通函数 |
5.507 |
5.476 |
5.491 |
5.491 |
133.7% |
inline普通函数 |
5.491 |
5.491 |
5.507 |
5.496 |
133.8% |
传引用函数 |
3.853 |
3.837 |
3.853 |
3.848 |
93.7% |
inline传引用函数 |
3.838 |
3.838 |
3.838 |
3.838 |
93.4% |
define |
2.075 |
2.044 |
2.090 |
2.070 |
50.4% |
long long 表达式
测试次数 |
Test 1 |
Test 2 |
Test 3 |
Avg |
百分比 |
for循环 |
2.090 |
2.121 |
2.106 |
2.106 |
11.0% |
STL |
19.188 |
19.142 |
19.157 |
19.162 |
100.0% |
普通函数 |
20.358 |
20.046 |
20.062 |
20.155 |
105.2% |
inline普通函数 |
20.265 |
20.233 |
20.109 |
20.202 |
105.4% |
define |
16.224 |
16.224 |
16.208 |
16.219 |
84.6% |
double 单变量
测试次数 |
Test 1 |
Test 2 |
Test 3 |
Avg |
百分比 |
for循环 |
2.074 |
2.091 |
2.091 |
2.085 |
60.7% |
STL |
3.448 |
3.447 |
3.416 |
3.437 |
100.0% |
普通函数 |
10.920 |
10.936 |
10.905 |
10.920 |
317.7% |
inline普通函数 |
10.873 |
10.982 |
10.873 |
10.909 |
317.4% |
传引用函数 |
3.230 |
3.230 |
3.229 |
3.230 |
94.0% |
inline传引用函数 |
4.321 |
4.352 |
4.337 |
4.337 |
126.2% |
define |
2.402 |
2.402 |
2.434 |
2.413 |
70.2% |
double 表达式
测试次数 |
Test 1 |
Test 2 |
Test 3 |
Avg |
百分比 |
for循环 |
2.278 |
2.122 |
2.122 |
2.174 |
47.8% |
STL |
4.555 |
4.555 |
4.539 |
4.550 |
100.0% |
普通函数 |
11.606 |
11.591 |
11.591 |
11.596 |
254.9% |
inline普通函数 |
11.654 |
11.591 |
11.654 |
11.633 |
255.7% |
define |
2.637 |
2.621 |
2.621 |
2.626 |
57.7% |
包含适量变量的struct 单变量
测试次数 |
Test 1 |
Test 2 |
Test 3 |
Avg |
百分比 |
for循环 |
2.153 |
2.121 |
2.106 |
2.127 |
17.2% |
STL |
12.621 |
12.246 |
12.231 |
12.366 |
100.0% |
普通函数 |
26.863 |
26.271 |
26.286 |
26.473 |
214.1% |
inline普通函数 |
26.317 |
26.302 |
26.349 |
26.323 |
212.9% |
传引用函数 |
17.800 |
17.862 |
17.799 |
17.820 |
144.1% |
inline传引用函数 |
17.831 |
17.753 |
17.769 |
17.784 |
143.8% |
define |
10.078 |
10.031 |
10.046 |
10.052 |
81.3% |
包含适量变量的struct 表达式
测试次数 |
Test 1 |
Test 2 |
Test 3 |
Avg |
百分比 |
for循环 |
2.247 |
2.121 |
2.106 |
2.158 |
3.8% |
STL |
57.330 |
57.268 |
57.627 |
57.408 |
100.0% |
普通函数 |
70.824 |
71.527 |
71.776 |
71.376 |
124.3% |
inline普通函数 |
70.887 |
71.324 |
71.183 |
71.131 |
123.9% |
define |
78.110 |
77.205 |
77.985 |
77.767 |
135.5% |
总结
- STL最稳定
- define在大部分情况是最快的
- define主要耗时在计算表达式上
- 如果创建当前类型的代价过高,传引用的函数相对不传的有明显优势
- 只要不去卡常数,用STL自带的就行了
源代码
#include
#include
#include
#include
#include
#include
using namespace std;
#define N 1010
#define PLL 9999999999999937LL
#define P 999999937
#define foru(i,l,r) for (int i=l; i<=r; i++)
#define ford(i,r,l) for (int i=r; i>=l; i--)
#define read(x) (x=getint())
#define minorz(a,b) ((a)<(b)?(a):(b))
typedef long long LL;
struct stt
{
int x;
double z;
LL e;
char f;
bool operator < (const stt a) const
{
return xoperator + (stt a)
{
stt r=a;
r.x+=x;
return r;
}
stt operator * (stt a)
{
stt r=a;
r.x+=x;
return r;
}
};
typedef int integer;
integer a[1000],b,c,d,y,z;
int x;
integer minfuc(integer a, integer b)
{
return areturn ainline integer inlminfuc(integer a, integer b)
{
return ainline integer inlminfuc2(integer &a, integer &b)
{
return a