如何优化长时间循环

时间:2016-01-09 13:42:57

标签: swift performance

这是我的循环代码:

    let entyLimitLength = 256  // word enty must smaller than 256

    let sizeOfWordDataOffset = 4 // 4 bytes or 8 byte if 64 bit

    let sizeOfWordDataSize = 4 //4 bytes or 8 byte if 64 bit


    var entyLocation = 0
    var entyLength = 0
    var searchRange:NSRange
    var subDataEnty:NSData

    var wordDataOffset:UInt32 = 0

    var wordDataSize:UInt32 = 0


    var rslt = [(String,NSData)]()
while entyLocation < idxData.length{
                searchRange = idxData.rangeOfData(nulTerminateCharacter, options: searchOption, range: NSMakeRange(entyLocation, min(entyLimitLength,(idxData.length - entyLocation))))
                if searchRange.length == 0 {
                    break // not match
                }

                entyLength = searchRange.location - entyLocation

                // this is what data sequene we need
                subDataEnty = idxData.subdataWithRange(NSMakeRange(entyLocation, entyLength))

                //point to offset number 's data sequene
                entyLocation = searchRange.location + searchRange.length

                //use pointer to get result @@
                idxData.getBytes(&wordDataOffset, range: NSMakeRange(entyLocation,sizeOfWordDataOffset))

                //swap Big endian to Litter endian (host)
                wordDataOffset = CFSwapInt32BigToHost(wordDataOffset)

                //point to word data size's data sequene
                entyLocation = entyLocation + sizeOfWordDataOffset

                //the same for word data size
                idxData.getBytes(&wordDataSize, range: NSMakeRange(entyLocation,sizeOfWordDataSize))

                wordDataSize = CFSwapInt32BigToHost(wordDataSize)

                //point to next data sequene
                entyLocation = entyLocation + sizeOfWordDataSize



                rslt = rslt + [(NSString(data: subDataEnty, encoding: NSUTF8StringEncoding) as! String , dictData.subdataWithRange(NSMakeRange(Int(wordDataOffset), Int(wordDataSize))))]


            }
            return rslt

采用NSData表单文件并将其更改为[(String,NSData)]数组 这个while循环运行超过200000次。在Xcode Simulation中需要花费太多时间。我不认为它需要太长时间。有什么理由让它变慢吗?

并且请不要对新手问题投票。

0 个答案:

没有答案