生活随笔
收集整理的這篇文章主要介紹了
c++ 各种求min/max方法效率测试
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
由于min跟max是等價(jià)的,下面只對(duì)求min進(jìn)行測(cè)試
每次測(cè)試執(zhí)行min 109次,記錄時(shí)間(單位:秒)
耗時(shí)以STL為參考基準(zhǔn)
運(yùn)行環(huán)境
Windows 7 32-bit on Intel Pentium G2030 @ 3.0GHz
編譯器
MinGW GCC 4.8.1 32-bit Release
附加命令
-g -Wall -Wl,-stack,100000000
int 單變量
測(cè)試次數(shù)Test 1Test 2Test 3Avg百分比
| for循環(huán) | 2.121 | 2.106 | 2.090 | 2.106 | 56.2% |
| STL | 3.744 | 3.744 | 3.744 | 3.744 | 100.0% |
| 普通函數(shù) | 3.386 | 3.385 | 3.401 | 3.391 | 90.6% |
| inline普通函數(shù) | 3.432 | 3.448 | 3.401 | 3.427 | 91.5% |
| 傳引用函數(shù) | 3.229 | 3.073 | 3.058 | 3.120 | 83.3% |
| inline傳引用函數(shù) | 3.447 | 3.401 | 3.400 | 3.416 | 91.2% |
| define | 2.387 | 2.387 | 2.403 | 2.392 | 63.9% |
int 表達(dá)式
測(cè)試次數(shù)Test 1Test 2Test 3Avg百分比
| for循環(huán) | 2.091 | 2.090 | 2.106 | 2.096 | 30.0% |
| STL | 7.004 | 6.973 | 6.973 | 6.983 | 100.0% |
| 普通函數(shù) | 6.178 | 6.178 | 6.178 | 6.178 | 88.5% |
| inline普通函數(shù) | 6.162 | 6.162 | 6.162 | 6.162 | 88.2% |
| define | 5.273 | 5.288 | 5.289 | 5.283 | 75.7% |
long long 單變量
測(cè)試次數(shù)Test 1Test 2Test 3Avg百分比
| for循環(huán) | 2.169 | 2.090 | 2.106 | 2.122 | 51.6% |
| STL | 4.118 | 4.103 | 4.103 | 4.108 | 100.0% |
| 普通函數(shù) | 5.507 | 5.476 | 5.491 | 5.491 | 133.7% |
| inline普通函數(shù) | 5.491 | 5.491 | 5.507 | 5.496 | 133.8% |
| 傳引用函數(shù) | 3.853 | 3.837 | 3.853 | 3.848 | 93.7% |
| inline傳引用函數(shù) | 3.838 | 3.838 | 3.838 | 3.838 | 93.4% |
| define | 2.075 | 2.044 | 2.090 | 2.070 | 50.4% |
long long 表達(dá)式
測(cè)試次數(shù)Test 1Test 2Test 3Avg百分比
| for循環(huán) | 2.090 | 2.121 | 2.106 | 2.106 | 11.0% |
| STL | 19.188 | 19.142 | 19.157 | 19.162 | 100.0% |
| 普通函數(shù) | 20.358 | 20.046 | 20.062 | 20.155 | 105.2% |
| inline普通函數(shù) | 20.265 | 20.233 | 20.109 | 20.202 | 105.4% |
| define | 16.224 | 16.224 | 16.208 | 16.219 | 84.6% |
double 單變量
測(cè)試次數(shù)Test 1Test 2Test 3Avg百分比
| for循環(huán) | 2.074 | 2.091 | 2.091 | 2.085 | 60.7% |
| STL | 3.448 | 3.447 | 3.416 | 3.437 | 100.0% |
| 普通函數(shù) | 10.920 | 10.936 | 10.905 | 10.920 | 317.7% |
| inline普通函數(shù) | 10.873 | 10.982 | 10.873 | 10.909 | 317.4% |
| 傳引用函數(shù) | 3.230 | 3.230 | 3.229 | 3.230 | 94.0% |
| inline傳引用函數(shù) | 4.321 | 4.352 | 4.337 | 4.337 | 126.2% |
| define | 2.402 | 2.402 | 2.434 | 2.413 | 70.2% |
double 表達(dá)式
測(cè)試次數(shù)Test 1Test 2Test 3Avg百分比
| for循環(huán) | 2.278 | 2.122 | 2.122 | 2.174 | 47.8% |
| STL | 4.555 | 4.555 | 4.539 | 4.550 | 100.0% |
| 普通函數(shù) | 11.606 | 11.591 | 11.591 | 11.596 | 254.9% |
| inline普通函數(shù) | 11.654 | 11.591 | 11.654 | 11.633 | 255.7% |
| define | 2.637 | 2.621 | 2.621 | 2.626 | 57.7% |
包含適量變量的struct 單變量
測(cè)試次數(shù)Test 1Test 2Test 3Avg百分比
| for循環(huán) | 2.153 | 2.121 | 2.106 | 2.127 | 17.2% |
| STL | 12.621 | 12.246 | 12.231 | 12.366 | 100.0% |
| 普通函數(shù) | 26.863 | 26.271 | 26.286 | 26.473 | 214.1% |
| inline普通函數(shù) | 26.317 | 26.302 | 26.349 | 26.323 | 212.9% |
| 傳引用函數(shù) | 17.800 | 17.862 | 17.799 | 17.820 | 144.1% |
| inline傳引用函數(shù) | 17.831 | 17.753 | 17.769 | 17.784 | 143.8% |
| define | 10.078 | 10.031 | 10.046 | 10.052 | 81.3% |
包含適量變量的struct 表達(dá)式
測(cè)試次數(shù)Test 1Test 2Test 3Avg百分比
| for循環(huán) | 2.247 | 2.121 | 2.106 | 2.158 | 3.8% |
| STL | 57.330 | 57.268 | 57.627 | 57.408 | 100.0% |
| 普通函數(shù) | 70.824 | 71.527 | 71.776 | 71.376 | 124.3% |
| inline普通函數(shù) | 70.887 | 71.324 | 71.183 | 71.131 | 123.9% |
| define | 78.110 | 77.205 | 77.985 | 77.767 | 135.5% |
總結(jié)
- STL最穩(wěn)定
- define在大部分情況是最快的
- define主要耗時(shí)在計(jì)算表達(dá)式上
- 如果創(chuàng)建當(dāng)前類(lèi)型的代價(jià)過(guò)高,傳引用的函數(shù)相對(duì)不傳的有明顯優(yōu)勢(shì)
- 只要不去卡常數(shù),用STL自帶的就行了
源代碼
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <iostream>
#include <windows.h>
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 x<a.x;}stt
operator + (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 a<b?a:b;
}
integer minfuc2(integer &a, integer &b)
{
return a<b?a:b;
}
inline integer inlminfuc(integer a, integer b)
{
return a<b?a:b;
}
inline integer inlminfuc2(integer &a, integer &b)
{
return a<b?a:b;
}
#define val1 a[233]
#define val2 a[666]
int main()
{a[
233]=
233333333<<
8, a[
666]=
666666666<<
7;
foru(i,
1,
100) a[i]=i;
x=GetTickCount();foru(i,
1,
1000000000){ }
printf(
"for cost %dms\n",GetTickCount()-x);
x=GetTickCount();foru(i,
1,
1000000000){y=min(val1,val2); }
printf(
"STL cost %dms\n",GetTickCount()-x);
x=GetTickCount();foru(i,
1,
1000000000){y=minfuc(val1,val2);}
printf(
"function cost %dms\n",GetTickCount()-x);
x=GetTickCount();foru(i,
1,
1000000000){y=inlminfuc(val1,val2); }
printf(
"inline function cost %dms\n",GetTickCount()-x);
x=GetTickCount();foru(i,
1,
1000000000){y=minfuc2(val1,val2);}
printf(
"function2 cost %dms\n",GetTickCount()-x);
x=GetTickCount();foru(i,
1,
1000000000){y=inlminfuc2(val1,val2); }
printf(
"inline function2 cost %dms\n",GetTickCount()-x);
x=GetTickCount();foru(i,
1,
1000000000){y=minorz(val1,val2); }
printf(
"define cost %dms\n",GetTickCount()-x);
return 0;
}
總結(jié)
以上是生活随笔為你收集整理的c++ 各种求min/max方法效率测试的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。