[swift-users] Why are Swift Loops slow?

Gerriet M. Denkmann g at mdenkmann.de
Wed Oct 12 04:25:55 CDT 2016


uint64_t nbrBytes = 4e8;
uint64_t count = 0;
for( uint64_t byteIndex = 0; byteIndex < nbrBytes; byteIndex++ )
{
	count += byteIndex;
	if ( ( byteIndex & 0xffffffff ) == 0 ) { count += 1.3; }  (AAA) 
};

Takes 260 msec.

Btw.: Without the (AAA) line the whole loop is done in 10 μsec. A really clever compiler!
And with “count += 1” instead of “count += 1.3” it takes 410 msec. Very strange. 
But this is beside the point here.


Now Swift:
let nbrBytes = 400_000_000
var count = 0
for byteIndex in 0 ..< nbrBytes
{
	count += byteIndex
	if ( ( byteIndex & 0xffffffff ) == 0 ) {count += Int(1.3);}
}

takes 390 msec - about 50 % more.

Release build with default options.

Gerriet.



More information about the swift-users mailing list