[swift-users] Looping with UInt64

Gerriet M. Denkmann g at mdenkmann.de
Wed Oct 12 01:04:07 CDT 2016


With:
let nbrBytes = 400_000_000
let localArray = UnsafeMutablePointer<UInt8>.allocate(capacity: nbrBytes)
//	touch every page before summing:
for i in 0 ..< nbrBytes / 4096 { localArray[ 4096 * i ] = 1 }

This rather innocent loop:
var count: UInt64 = 0
for index in 0 ..< loopLimit { count += UInt64( localArray[ Int(index) ] ) }

takes 2.4 times as long if preceded by:
	let loopLimit = UInt64(nbrBytes)	
compared to:	
	let loopLimit = Int(nbrBytes)	

Why can’t Swift iterate in UInt64  as efficient as with Int?
Any real issue here which I am overlooking or just a compiler bug?

Gerriet.



More information about the swift-users mailing list