: 推 Donnie:感覺 short 計算比較慢 11/02 01:52
: → Donnie:除非要省記憶體,不然不會用 short 11/02 01:52
實驗結果: short int 真的比較慢 = =
short int return function
[yellowfish@edasolaris c]$ g++ -O2 shortint.cpp; time ./a.out
real 0m7.552s
user 0m7.200s
sys 0m0.000s
int return function
[yellowfish@edasolaris c]$ g++ -O2 shortint.cpp; time ./a.out
real 0m5.462s
user 0m5.390s
sys 0m0.010s
測試程式:
#include <iostream>
#define SHORTINT 0
using namespace std;
#if SHORTINT
short int add1(int a, int b)
{
short int c = a + b;
return c;
}
#else
int add2(int a, int b)
{
int c = a + b;
return c;
}
#endif
int main()
{
int a, b;
#if SHORTINT
short int c;
#else
int c;
#endif
a = 2;
b = 4;
for (unsigned int i = 0; i < INT_MAX; i++)
{
#if SHORTINT
c = add1(a,b);
#else
c = add2(a,b);
#endif
}
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.25.218