求解惑,double 与 float 的运算速度测试,为什么是这样?
重复执行4次一样的代码,只有第一次的时候,double与float有明显区别,后面三次区别就很小了
#include <iostream>
#include <math.h>
#include <chrono>
using namespace std;
class TicToc
{
public:
TicToc(const std::string& name):m_name(name) { tic(); }
void tic() {
start = std::chrono::system_clock::now();
}
void toc() {
auto millinon_second = std::chrono::duration_cast<std::chrono::milliseconds>
(std::chrono::system_clock::now()-start);
std::cout << m_name << " cost " << millinon_second.count() << " ms." << std::endl;
}
private:
std::string m_name;
std::chrono::time_point<std::chrono::system_clock> start, end;
};
constexpr size_t N = 20000000;
double result_double[N];
float result_float[N];
int main()
{
float pi_float = 3.1415926f;
double pi_double = 3.1415926;
TicToc double_time("double multiply ");
for (size_t i = 0; i < N; ++i) {
result_double[i] = double(i) * pi_double;
}
double_time.toc();
TicToc float_time("float multiply ");
for (size_t i = 0; i < N; ++i) {
result_float[i] = float(i) * pi_float;
}
float_time.toc();
/////////////////////////////////////////
TicToc double_time2("double multiply 2");
for (size_t i = 0; i < N; ++i) {
result_double[i] = double(i) * pi_double;
}
double_time2.toc();
TicToc float_time2("float multiply 2");
for (size_t i = 0; i < N; ++i) {
result_float[i] = float(i) * pi_float;
}
float_time2.toc();
/////////////////////////////////////////
TicToc double_time3("double multiply 3");
for (size_t i = 0; i < N; ++i) {
result_double[i] = double(i) * pi_double;
}
double_time3.toc();
TicToc float_time3("float multiply 3");
for (size_t i = 0; i < N; ++i) {
result_float[i] = float(i) * pi_float;
}
float_time3.toc();
/////////////////////////////////////////
TicToc double_time4("double multiply 4");
for (size_t i = 0; i < N; ++i) {
result_double[i] = double(i) * pi_double;
}
double_time4.toc();
TicToc float_time4("float multiply 4");
for (size_t i = 0; i < N; ++i) {
result_float[i] = float(i) * pi_float;
}
float_time4.toc();
}
编译: g++ float_double.cpp -std=c++11 -O3
四次运行结果:
xxxx$ ./a.out
double multiply cost 73 ms.
float multiply cost 28 ms.
double multiply 2 cost 11 ms.
float multiply 2 cost 9 ms.
double multiply 3 cost 11 ms.
float multiply 3 cost 14 ms.
double multiply 4 cost 14 ms.
float multiply 4 cost 14 ms.
xxxx$ ./a.out
double multiply cost 51 ms.
float multiply cost 28 ms.
double multiply 2 cost 12 ms.
float multiply 2 cost 9 ms.
double multiply 3 cost 11 ms.
float multiply 3 cost 14 ms.
double multiply 4 cost 14 ms.
float multiply 4 cost 14 ms.
xxxx$ ./a.out
double multiply cost 51 ms.
float multiply cost 28 ms.
double multiply 2 cost 11 ms.
float multiply 2 cost 9 ms.
double multiply 3 cost 11 ms.
float multiply 3 cost 14 ms.
double multiply 4 cost 14 ms.
float multiply 4 cost 14 ms.
xxxx$ ./a.out
double multiply cost 78 ms.
float multiply cost 28 ms.
double multiply 2 cost 11 ms.
float multiply 2 cost 9 ms.
double multiply 3 cost 11 ms.
float multiply 3 cost 14 ms.
double multiply 4 cost 14 ms.
float multiply 4 cost 13 ms.
平台:Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
【 在 foliver 的大作中提到: 】
: 和寄存器指令有关最开始。
: 最开始浮点数都是fpu计算,fpu寄存器都是80bits大小,不管float还是double,没有区别,指令也一样。
: 后来2000年前后,intel推广sse,引入xmm寄存器,遵从float/double的大小,使用不同的指令。这时的float才是真float。
: ...................
--
FROM 14.155.19.*