IBM Swift Sandbox无法解析对CoreFoundation函数

时间:2015-12-05 12:57:59

标签: swift ibm-swift-sandbox

我正在使用IBM Swift Sandbox的测试版;有谁知道为什么我的代码出现以下错误?:

LLVM ERROR:程序使用了无法解析的外部函数'CFAbsoluteTimeGetCurrent'!

// A demonstration of both iterative and recursive algorithms for computing the Fibonacci numbers.

import CoreFoundation

// A recursive algorithm to compute the Fibonacci numbers.
func fibRec (n : Int) -> Double {
    return (Double)(n < 3 ? 1 : fibRec(n - 1) + fibRec(n - 2))
}

// An iterative algorithm to compute the Fibonacci numbers.
func fibIter (n : Int) -> Double {
    var f2 = 0.0
    var f1 = 1.0
    var f0 = 1.0

    for _ in 0 ..< n {
        f2 = f1 + f0
        f0 = f1
        f1 = f2
    }
    return f0
}

// Initialise array to hold algorithm execution times.
var fibTimes = [Double]()

// i is the ith Fibonacci number to be computed.
for i in 120..<129 {

    var fibNum = 0.0
    var fibSum = 0.0

    // j is the number of times to compute F(i) to obtain average.
    for j in 0..<5 {

        // Set start time.
        let startTime = CFAbsoluteTimeGetCurrent()

        // Uses the recursive algorithm.
        //                fibNum = fibRec(i)

        // Uses the iterative algorithm.
        fibNum = fibIter(i)
        fibTimes.insert(CFAbsoluteTimeGetCurrent() - startTime, atIndex: j)
    }

    // Compute the average execution time.
    for p in fibTimes {
        fibSum += p
    }
    fibSum = fibSum / 5

    print("Fibonacci number \(i) is: \(fibNum)")
    print("Execution time:         \(fibSum) seconds")
}

2 个答案:

答案 0 :(得分:3)

你也必须import Foundation

我不确定为什么需要这样做(我希望有人知道,可以对此有所了解),但它确实有效。

答案 1 :(得分:0)

澄清一下,你必须同时导入它们。

import Foundation
import CoreFoundation

或者,Glibc有clock(),它返回一个Int。例如:

import Glibc
struct StartTimer {
    let start = clock()
    var elapsed = -1
    mutating func stop() { elapsed = clock() - start }
}

var timer = StartTimer()
/* do your work */
timer.stop()
print("Elapsed Time: \(timer.elapsed)")