IBM Swift 沙盒无法解析对 CoreFoundation 函数的调用
IBM Swift Sandbox cannot resolve call to CoreFoundation function
我正在研究 IBM Swift Sandbox 的测试版;有谁知道为什么我收到以下代码的以下错误?:
LLVM 错误:程序使用了无法解析的外部函数 '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")
}
您也 import Foundation
。
我不确定为什么需要这样做(我希望知道的人可以阐明这一点),但它确实有效。
澄清一下,您必须将它们都导入。
import Foundation
import CoreFoundation
或者,Glibc 有 clock() 而 returns 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)")
我正在研究 IBM Swift Sandbox 的测试版;有谁知道为什么我收到以下代码的以下错误?:
LLVM 错误:程序使用了无法解析的外部函数 '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")
}
您也 import Foundation
。
我不确定为什么需要这样做(我希望知道的人可以阐明这一点),但它确实有效。
澄清一下,您必须将它们都导入。
import Foundation
import CoreFoundation
或者,Glibc 有 clock() 而 returns 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)")