float, int , int64计算性能

来源:互联网 发布:vm虚拟机下载 for mac 编辑:程序博客网 时间:2024/06/08 12:25

虽然说有了FPU和各种扩展指令集后,盛传浮点数的计算性能已经和整形非常接近了,只有几倍的CPU周期差距了。甚至都是一样1~2个CPU周期就计算完毕了。还有x64的cpu出来后,x64得寄存器对于64位的计算和32位的计算也是性能几乎相同了。

时间是检验真理的唯一标准,现在实验如下:

CPU: Intel(R) Core(TM)2 Duo CPU     E7400  @ 2.80GHz (2 CPUs), ~2.8GHz

Mem:4990MB RAM

GPU:NVIDIA GeForce GT 630

编译:

/Zi /nologo /W3 /WX- /O2 /Oi /Oy- /GL /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /Gm- /EHsc /GS /Gy /fp:precise /Zc:wchar_t /Zc:forScope /Fp"Release\CalcTest.pch" /Fa"Release\" /Fo"Release\" /Fd"Release\vc100.pdb" /Gd /analyze- /errorReport:queue 

win32

/OUT:"D:\Code\VS2010Test\Release\CalcTest.exe" /INCREMENTAL:NO /NOLOGO "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /MANIFEST /ManifestFile:"Release\CalcTest.exe.intermediate.manifest" /ALLOWISOLATION /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"D:\Code\VS2010Test\Release\CalcTest.pdb" /SUBSYSTEM:CONSOLE /OPT:REF /OPT:ICF /PGD:"D:\Code\VS2010Test\Release\CalcTest.pgd" /LTCG /TLBID:1 /DYNAMICBASE /NXCOMPAT /MACHINE:X86 /ERRORREPORT:QUEUE 

win64

/OUT:"D:\Code\VS2010Test\x64\Release\CalcTest.exe" /INCREMENTAL:NO /NOLOGO "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /MANIFEST /ManifestFile:"x64\Release\CalcTest.exe.intermediate.manifest" /ALLOWISOLATION /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"D:\Code\VS2010Test\x64\Release\CalcTest.pdb" /SUBSYSTEM:CONSOLE /OPT:REF /OPT:ICF /PGD:"D:\Code\VS2010Test\x64\Release\CalcTest.pgd" /LTCG /TLBID:1 /DYNAMICBASE /NXCOMPAT /MACHINE:X64 /ERRORREPORT:QUEUE 

代码:

#include <stdio.h>#include <Windows.h>#define NUM 100000000#define FILE_NAME "CalcTest.log"void TestPrint(char *pFormat, ...){FILE *pFile = fopen(FILE_NAME, "a");va_list vaList;va_start(vaList, pFormat);vfprintf(pFile, pFormat, vaList);fclose(pFile);}void main(){float f[5] = {1.1, 1.2, 1.3, 1.5, 1.6};int i[5] = {1, 2, 3, 4, 5};long long l[5] = {1, 2, 3, 4, 5};DWORD iB = GetTickCount();for(int j = 0; j < NUM; j++){i[j%4] = i[(j+1)%4] + i[(j+2)%4];}DWORD iE = GetTickCount();TestPrint("int = int + int %d count cost %d ms\n", NUM, iE - iB);iB = GetTickCount();for(int j = 0; j < NUM; j++){l[j%4] = l[(j+1)%4] + l[(j+2)%4];}iE = GetTickCount();TestPrint("i64 = i64 + i64 %d count cost %d ms\n", NUM, iE - iB);iB = GetTickCount();for(int j = 0; j < NUM; j++){l[j%4] = i[(j+1)%4] + l[(j+2)%4];}iE = GetTickCount();TestPrint("i64 = int + i64 %d count cost %d ms\n", NUM, iE - iB);iB = GetTickCount();for(int j = 0; j < NUM; j++){l[j%4] = l[(j+1)%4] + i[(j+2)%4];}iE = GetTickCount();TestPrint("i64 = i64 + int %d count cost %d ms\n", NUM, iE - iB);iB = GetTickCount();for(int j = 0; j < NUM; j++){f[j%4] = f[(j+1)%4] + f[(j+2)%4];}iE = GetTickCount();TestPrint("float = float + float %d count cost %d ms\n", NUM, iE - iB);iB = GetTickCount();for(int j = 0; j < NUM; j++){f[j%4] = i[(j+1)%4] + f[(j+2)%4];}iE = GetTickCount();TestPrint("float = int + float %d count cost %d ms\n", NUM, iE - iB);iB = GetTickCount();for(int j = 0; j < NUM; j++){i[j%4] = i[(j+1)%4] + f[(j+2)%4];}iE = GetTickCount();TestPrint("int = int + float %d count cost %d ms\n", NUM, iE - iB);iB = GetTickCount();for(int j = 0; j < NUM; j++){f[j%4] = f[(j+1)%4] + i[(j+2)%4];}iE = GetTickCount();TestPrint("float = float + int %d count cost %d ms\n", NUM, iE - iB);/*****************************************************************************************/iB = GetTickCount();for(int j = 0; j < NUM; j++){i[4] = i[(j+1)%4] * i[(j+2)%4];}iE = GetTickCount();TestPrint("int = int * int %d count cost %d ms\n", NUM, iE - iB);iB = GetTickCount();for(int j = 0; j < NUM; j++){l[4] = l[(j+1)%4] * l[(j+2)%4];}iE = GetTickCount();TestPrint("i64 = i64 * i64 %d count cost %d ms\n", NUM, iE - iB);iB = GetTickCount();for(int j = 0; j < NUM; j++){l[4] = i[(j+1)%4] * l[(j+2)%4];}iE = GetTickCount();TestPrint("i64 = int * i64 %d count cost %d ms\n", NUM, iE - iB);iB = GetTickCount();for(int j = 0; j < NUM; j++){l[4] = l[(j+1)%4] * i[(j+2)%4];}iE = GetTickCount();TestPrint("i64 = i64 * int %d count cost %d ms\n", NUM, iE - iB);iB = GetTickCount();for(int j = 0; j < NUM; j++){f[4] = f[(j+1)%4] * f[(j+2)%4];}iE = GetTickCount();TestPrint("float = float * float %d count cost %d ms\n", NUM, iE - iB);iB = GetTickCount();for(int j = 0; j < NUM; j++){f[4] = i[(j+1)%4] * f[(j+2)%4];}iE = GetTickCount();TestPrint("float = int * float %d count cost %d ms\n", NUM, iE - iB);iB = GetTickCount();for(int j = 0; j < NUM; j++){i[4] = i[(j+1)%4] * f[(j+2)%4];}iE = GetTickCount();TestPrint("int = int * float %d count cost %d ms\n", NUM, iE - iB);iB = GetTickCount();for(int j = 0; j < NUM; j++){f[4] = f[(j+1)%4] * i[(j+2)%4];}iE = GetTickCount();TestPrint("float = float * int %d count cost %d ms\n", NUM, iE - iB);/*****************************************************************************************/iB = GetTickCount();for(int j = 0; j < NUM; j++){i[4] = i[(j+1)%4] / i[(j+2)%4];}iE = GetTickCount();TestPrint("int = int / int %d count cost %d ms\n", NUM, iE - iB);iB = GetTickCount();for(int j = 0; j < NUM; j++){l[4] = l[(j+1)%4] / l[(j+2)%4];}iE = GetTickCount();TestPrint("i64 = i64 / i64 %d count cost %d ms\n", NUM, iE - iB);iB = GetTickCount();for(int j = 0; j < NUM; j++){f[4] = f[(j+1)%4] / f[(j+2)%4];}iE = GetTickCount();TestPrint("float = float / float %d count cost %d ms\n", NUM, iE - iB);iB = GetTickCount();for(int j = 0; j < NUM; j++){f[4] = i[(j+1)%4] / f[(j+2)%4];}iE = GetTickCount();TestPrint("float = int / float %d count cost %d ms\n", NUM, iE - iB);iB = GetTickCount();for(int j = 0; j < NUM; j++){i[4] = i[(j+1)%4] / f[(j+2)%4];}iE = GetTickCount();TestPrint("int = int / float %d count cost %d ms\n", NUM, iE - iB);iB = GetTickCount();for(int j = 0; j < NUM; j++){f[4] = f[(j+1)%4] / i[(j+2)%4];}iE = GetTickCount();TestPrint("float = float / int %d count cost %d ms\n", NUM, iE - iB);/*****************************************************************************************/iB = GetTickCount();for(int j = 0; j < NUM; j++){i[4] = i[(j+1)%4] - i[(j+2)%4];}iE = GetTickCount();TestPrint("int = int - int %d count cost %d ms\n", NUM, iE - iB);iB = GetTickCount();for(int j = 0; j < NUM; j++){l[4] = l[(j+1)%4] - l[(j+2)%4];}iE = GetTickCount();TestPrint("i64 = i64 - i64 %d count cost %d ms\n", NUM, iE - iB);iB = GetTickCount();for(int j = 0; j < NUM; j++){l[4] = l[(j+1)%4] - i[(j+2)%4];}iE = GetTickCount();TestPrint("i64 = i64 - int %d count cost %d ms\n", NUM, iE - iB);iB = GetTickCount();for(int j = 0; j < NUM; j++){f[4] = f[(j+1)%4] - f[(j+2)%4];}iE = GetTickCount();TestPrint("float = float - float %d count cost %d ms\n", NUM, iE - iB);iB = GetTickCount();for(int j = 0; j < NUM; j++){f[4] = i[(j+1)%4] - f[(j+2)%4];}iE = GetTickCount();TestPrint("float = int - float %d count cost %d ms\n", NUM, iE - iB);iB = GetTickCount();for(int j = 0; j < NUM; j++){i[4] = i[(j+1)%4] - f[(j+2)%4];}iE = GetTickCount();TestPrint("int = int - float %d count cost %d ms\n", NUM, iE - iB);iB = GetTickCount();for(int j = 0; j < NUM; j++){f[4] = f[(j+1)%4] - i[(j+2)%4];}iE = GetTickCount();TestPrint("float = float - int %d count cost %d ms\n", NUM, iE - iB);}

最终结果:

win32编译

int = int + int 100000000 count cost 156 ms
i64 = i64 + i64 100000000 count cost 250 ms
i64 = int + i64 100000000 count cost 187 ms
i64 = i64 + int 100000000 count cost 188 ms
float = float + float 100000000 count cost 11638 ms
float = int + float 100000000 count cost 11419 ms
int = int + float 100000000 count cost 11700 ms
float = float + int 100000000 count cost 11186 ms
int = int * int 100000000 count cost 156 ms
i64 = i64 * i64 100000000 count cost 702 ms
i64 = int * i64 100000000 count cost 748 ms
i64 = i64 * int 100000000 count cost 734 ms
float = float * float 100000000 count cost 11076 ms
float = int * float 100000000 count cost 11076 ms
int = int * float 100000000 count cost 11388 ms
float = float * int 100000000 count cost 11108 ms
int = int / int 100000000 count cost 405 ms
i64 = i64 / i64 100000000 count cost 7941 ms
float = float / float 100000000 count cost 12386 ms
float = int / float 100000000 count cost 11482 ms
int = int / float 100000000 count cost 11528 ms
float = float / int 100000000 count cost 8440 ms
int = int - int 100000000 count cost 140 ms
i64 = i64 - i64 100000000 count cost 156 ms
i64 = i64 - int 100000000 count cost 188 ms
float = float - float 100000000 count cost 12152 ms
float = int - float 100000000 count cost 11014 ms
int = int - float 100000000 count cost 11435 ms
float = float * int 100000000 count cost 8283 ms
int = int + int 100000000 count cost 141 ms
i64 = i64 + i64 100000000 count cost 265 ms
i64 = int + i64 100000000 count cost 265 ms
i64 = i64 + int 100000000 count cost 296 ms
float = float + float 100000000 count cost 11232 ms
float = int + float 100000000 count cost 10686 ms
int = int + float 100000000 count cost 11264 ms
float = float + int 100000000 count cost 10935 ms
int = int * int 100000000 count cost 141 ms
i64 = i64 * i64 100000000 count cost 686 ms
i64 = int * i64 100000000 count cost 718 ms
i64 = i64 * int 100000000 count cost 733 ms
float = float * float 100000000 count cost 10795 ms
float = int * float 100000000 count cost 10655 ms
int = int * float 100000000 count cost 11232 ms
float = float * int 100000000 count cost 10998 ms
int = int / int 100000000 count cost 500 ms
i64 = i64 / i64 100000000 count cost 7924 ms
float = float / float 100000000 count cost 11560 ms
float = int / float 100000000 count cost 11123 ms
int = int / float 100000000 count cost 11388 ms
float = float / int 100000000 count cost 8330 ms
int = int - int 100000000 count cost 125 ms
i64 = i64 - i64 100000000 count cost 140 ms
i64 = i64 - int 100000000 count cost 203 ms
float = float - float 100000000 count cost 11809 ms
float = int - float 100000000 count cost 11030 ms
int = int - float 100000000 count cost 11419 ms
float = float - int 100000000 count cost 9251 ms
int = int + int 100000000 count cost 187 ms
i64 = i64 + i64 100000000 count cost 296 ms
i64 = int + i64 100000000 count cost 219 ms
i64 = i64 + int 100000000 count cost 219 ms
float = float + float 100000000 count cost 11950 ms
float = int + float 100000000 count cost 11045 ms
int = int + float 100000000 count cost 12012 ms
float = float + int 100000000 count cost 11513 ms
int = int * int 100000000 count cost 171 ms
i64 = i64 * i64 100000000 count cost 812 ms
i64 = int * i64 100000000 count cost 733 ms
i64 = i64 * int 100000000 count cost 749 ms
float = float * float 100000000 count cost 11825 ms
float = int * float 100000000 count cost 11482 ms
int = int * float 100000000 count cost 11778 ms
float = float * int 100000000 count cost 11186 ms
int = int / int 100000000 count cost 405 ms
i64 = i64 / i64 100000000 count cost 8378 ms
float = float / float 100000000 count cost 12386 ms
float = int / float 100000000 count cost 11622 ms
int = int / float 100000000 count cost 12433 ms
float = float / int 100000000 count cost 8861 ms
int = int - int 100000000 count cost 156 ms
i64 = i64 - i64 100000000 count cost 156 ms
i64 = i64 - int 100000000 count cost 187 ms
float = float - float 100000000 count cost 12808 ms
float = int - float 100000000 count cost 12246 ms
int = int - float 100000000 count cost 12355 ms
float = float - int 100000000 count cost 9142 ms

win64编译

int = int + int 100000000 count cost 156 ms
i64 = i64 + i64 100000000 count cost 140 ms
i64 = int + i64 100000000 count cost 141 ms
i64 = i64 + int 100000000 count cost 156 ms
float = float + float 100000000 count cost 171 ms
float = int + float 100000000 count cost 156 ms
int = int + float 100000000 count cost 203 ms
float = float + int 100000000 count cost 141 ms
int = int * int 100000000 count cost 156 ms
i64 = i64 * i64 100000000 count cost 124 ms
i64 = int * i64 100000000 count cost 141 ms
i64 = i64 * int 100000000 count cost 125 ms
float = float * float 100000000 count cost 124 ms
float = int * float 100000000 count cost 125 ms
int = int * float 100000000 count cost 141 ms
float = float * int 100000000 count cost 140 ms
int = int / int 100000000 count cost 374 ms
i64 = i64 / i64 100000000 count cost 1217 ms
float = float / float 100000000 count cost 187 ms
float = int / float 100000000 count cost 188 ms
int = int / float 100000000 count cost 187 ms
float = float / int 100000000 count cost 187 ms
int = int - int 100000000 count cost 109 ms
i64 = i64 - i64 100000000 count cost 141 ms
i64 = i64 - int 100000000 count cost 156 ms
float = float - float 100000000 count cost 124 ms
float = int - float 100000000 count cost 125 ms
int = int - float 100000000 count cost 141 ms
float = float - int 100000000 count cost 124 ms
int = int + int 100000000 count cost 156 ms
i64 = i64 + i64 100000000 count cost 140 ms
i64 = int + i64 100000000 count cost 140 ms
i64 = i64 + int 100000000 count cost 156 ms
float = float + float 100000000 count cost 188 ms
float = int + float 100000000 count cost 171 ms
int = int + float 100000000 count cost 187 ms
float = float + int 100000000 count cost 141 ms
int = int * int 100000000 count cost 156 ms
i64 = i64 * i64 100000000 count cost 140 ms
i64 = int * i64 100000000 count cost 141 ms
i64 = i64 * int 100000000 count cost 124 ms
float = float * float 100000000 count cost 125 ms
float = int * float 100000000 count cost 125 ms
int = int * float 100000000 count cost 140 ms
float = float * int 100000000 count cost 172 ms
int = int / int 100000000 count cost 546 ms
i64 = i64 / i64 100000000 count cost 1248 ms
float = float / float 100000000 count cost 187 ms
float = int / float 100000000 count cost 187 ms
int = int / float 100000000 count cost 188 ms
float = float / int 100000000 count cost 187 ms
int = int - int 100000000 count cost 110 ms
i64 = i64 - i64 100000000 count cost 156 ms
i64 = i64 - int 100000000 count cost 156 ms
float = float - float 100000000 count cost 124 ms
float = int - float 100000000 count cost 141 ms
int = int - float 100000000 count cost 140 ms
float = float - int 100000000 count cost 141 ms
int = int + int 100000000 count cost 140 ms
i64 = i64 + i64 100000000 count cost 156 ms
i64 = int + i64 100000000 count cost 141 ms
i64 = i64 + int 100000000 count cost 140 ms
float = float + float 100000000 count cost 172 ms
float = int + float 100000000 count cost 171 ms
int = int + float 100000000 count cost 187 ms
float = float + int 100000000 count cost 141 ms
int = int * int 100000000 count cost 156 ms
i64 = i64 * i64 100000000 count cost 125 ms
i64 = int * i64 100000000 count cost 140 ms
i64 = i64 * int 100000000 count cost 125 ms
float = float * float 100000000 count cost 125 ms
float = int * float 100000000 count cost 125 ms
int = int * float 100000000 count cost 140 ms
float = float * int 100000000 count cost 125 ms
int = int / int 100000000 count cost 390 ms
i64 = i64 / i64 100000000 count cost 1201 ms
float = float / float 100000000 count cost 188 ms
float = int / float 100000000 count cost 187 ms
int = int / float 100000000 count cost 187 ms
float = float / int 100000000 count cost 187 ms
int = int - int 100000000 count cost 109 ms
i64 = i64 - i64 100000000 count cost 141 ms
i64 = i64 - int 100000000 count cost 156 ms
float = float - float 100000000 count cost 125 ms
float = int - float 100000000 count cost 140 ms
int = int - float 100000000 count cost 125 ms
float = float - int 100000000 count cost 140 ms


总结:都已int32为标准,结果是与int32的比值

win32比较:

+: int32: 1 int64:2 float:70

-: int32: 1 int64: 1 float:70

*: int32: 1 int64: 5 float:70

/: int32: 1 int64:20 float:30

同种类型对比:

int32: +、-、*性能几乎相同,\是其他的3~4倍

int64: -性能最好,+是-的1.7倍,* 是-的5倍,\是-的50倍

float:+、-、*、/性能几乎相同


win64比较:

+: int32: 1 int64:1 float:1

-: int32: 1 int64: 1 float:1

*: int32: 1 int64: 1 float:1

/: int32: 1 int64:3 float:0.5

同种类型对比:

int32: +、-、*性能几乎相同,\是其他的3~4倍

int64: +、-、*性能几乎相同,\是其他的6~7倍

float:+、-、*、/性能几乎相同


win32与win64对比

int32两种几乎相同

int64性能得到极大的提升,但是/的计算还是相对有些差距

float性能提升了差不多70倍


原创粉丝点击