android double float,android - Float or Double? - Stack Overflow

I wondered about this too and wrote a small test:

#include

#include

template

void test(void) {

std::cout<< "Size of variable: " << sizeof(numType) << std::endl;

numType array[20000];

auto t1 = std::chrono::high_resolution_clock::now();

// fill array

for( numType& number : array ) {

number = 1.0014535;

}

auto t2 = std::chrono::high_resolution_clock::now();

// multiply each number with itself 10.000 times

for( numType& number : array ) {

for( int i=0; i < 10000 ; i++ ) {

number *= number;

}

}

auto t3 = std::chrono::high_resolution_clock::now();

auto filltime = t2 - t1;

auto calctime = t3 - t2;

std::cout<< "Fill time: " << filltime.count() << std::endl;

std::cout<< "Calc time: " << calctime.count() << std::endl;

}

int main(int argc, char* argv[]) {

test();

test();

}

I ran and compiled it under Ubuntu 12.04 x64 using GCC on an Intel i7 3930k processor

These were the results:

Size of variable: 4

Fill time: 69

Calc time: 694303

Size of variable: 8

Fill time: 76

Calc time: 693363

Results were reproducable. So memory allocation for double takes slightly longer but the actual calculation time is exactly the same.

Out of curiousity I also ran and compiled it under Windows 7 x64 using Visual Studio 2012 in release mode on an intel i7 920 processor

(The unit of the time is different so don't compare the results above to these: it is only valid for internal comparison)

Size of variable: 4

Fill time: 0

Calc time: 3200183

Size of variable: 8

Fill time: 0

Calc time: 3890223

Results were reproducable.

It seems on windows allocation is instant, perhaps because linux does not actually give you memory until you use it while windows just hands it all over to you at once, requiring less system calls. Or perhaps the assignment is optimized away.

The multiplication of doubles is 21,5% slower here than for floats. This difference with the previous test is likely due to the different processor (that's my best guess at least).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值