Re: Is Swift really swift?
Re: Is Swift really swift?
- Subject: Re: Is Swift really swift?
- From: "Gerriet M. Denkmann" <email@hidden>
- Date: Sun, 06 Jul 2014 11:45:00 +0700
On 6 Jul 2014, at 03:37, Quincey Morris <email@hidden> wrote:
> On Jul 5, 2014, at 12:48 , Gerriet M. Denkmann <email@hidden> wrote:
>
>> I have a heavily used array called maskArray ( maskArray[i] = 0x1 << i ).
>>
>> I summed this array 100 Mio times in Objective-C
>> Took 0.05 sec. This is 1.5 times faster than NOT using maskArray, instead computing the value directly (0.075 sec)
>> uint32_t maskArray[32];
>>
>> Did the same in Swift.
>> Using maskArray took 15 sec. This is 160 times slower than computing directly: 0.09 sec (1.25 slower than Obj-C).
>> And it is 300 times slower than Obj-C.
>>
>> var maskArray = UInt32[](count: Int(32), repeatedValue: 0)
>
> Perhaps you could post more of the Swift code?
Will do (see at bottom of this posting).
>
> Note that it took me a while to work out that when you said “Obj-C” you really meant “C”. That is, you’re using a C array, not a NSArray.
Correct. But this is something which can be done in Objective-C. And maybe it should be possible to do it in Swift as well.
> Instead the correct approach (assuming that a lookup-based performance optimization is called for) may be to get hold of some raw memory (e.g. a NSData object of suitable size) and index into its bytes directly.
Or CFBitVector as Jens suggested.
And here is my Swift code:
typealias CHUNK_TYPE = UInt32
let CHUNK_SIZE_IN_BYTES = UInt(sizeof(CHUNK_TYPE) )
let CHUNK_SIZE_IN_BITS = CHUNK_SIZE_IN_BYTES * 8
class TimingSwiftArrays
{
var maskArray = CHUNK_TYPE[](count: Int(CHUNK_SIZE_IN_BITS), repeatedValue: 0) // set in makeStaticData
init()
{
makeStaticData()
}
func swiftArray( nbr: UInt )
{
// swiftArray sum 13421770649391352 of 99999999 in 14.6993219852448 sec
// ca. 157 times slower
let startDate = NSDate()
var sum : UInt = 0
for i in 0..nbr
{
let n = i % CHUNK_SIZE_IN_BITS
let v = maskArray[ Int(n) ]
sum += UInt(v)
}
let elapsedTime = -startDate.timeIntervalSinceNow
println( "swiftArray sum \(sum) of \(nbr) in \(elapsedTime) sec" )
}
func directSum( nbr: UInt )
{
// directSum sum 13421770649391352 of 99999999 in 0.0932070016860962 sec
let startDate = NSDate()
let oneBit : UInt32 = 0x1
var sum : UInt = 0
for i in 0..nbr
{
let n = i % CHUNK_SIZE_IN_BITS
let shifter = UInt32(n) // shifter must NOT be UInt or Int
let v = oneBit << shifter
sum += UInt(v)
}
let elapsedTime = -startDate.timeIntervalSinceNow
println( "directSum sum \(sum) of \(nbr) in \(elapsedTime) sec" )
}
func makeStaticData()
{
let oneBit : CHUNK_TYPE = 0x1
for bIndex in 0..CHUNK_SIZE_IN_BITS
{
let shifter = UInt32(bIndex) // shifter must NOT be UInt or Int
maskArray[ Int(bIndex) ] = oneBit << shifter // bIndex must NOT be UInt
}
}
}
Gerriet.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden