lor*_*isi 9 c switch-statement fall-through duffs-device swift
我们知道Duff的设备使用隔行开关和循环的结构交错,如:
send(to, from, count)
register short *to, *from;
register count;
{
register n = (count + 7) / 8;
switch (count % 8) {
case 0: do { *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while (--n > 0);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,在Swif 2.1中,当我们在Swift文档中读到时,switch-case控制流并没有隐含地落后:
没有隐含的堕落
与C和Objective-C中的switch语句相比,Swift中的switch语句不会通过每个case的底部进入默认情况下的下一个语句.相反,只要第一个匹配的switch case完成,整个switch语句就会完成执行,而不需要显式的break语句.这使得switch语句比C语言更安全,更易于使用,并且避免错误地执行多个switch case.
现在,鉴于在Swift中明确存在一个突破性副作用,有一个通过条款:
下通
Swift中的切换语句不会落入每个案例的底部并进入下一个案例.相反,只要第一个匹配的案例完成,整个switch语句就会完成它的执行.相比之下,C要求您在每个开关盒的末尾插入一个显式的break语句,以防止出现问题.避免默认的下降意味着Swift switch语句比C中的对应语句更简洁和可预测,因此它们避免错误地执行多个switch case.
这非常像:
let integerToDescribe = 5
var description = "The number \(integerToDescribe) is"
switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
description += " a prime number, and also"
fallthrough
default:
description += " an integer."
}
print(description)
// prints "The number 5 is a prime number, and also an integer."
Run Code Online (Sandbox Code Playgroud)
考虑到维基百科提醒我们,这些设备来自这个问题
A straightforward code to copy items from an array to a memory-mapped output register might look like this:
do { /* count > 0 assumed */
*to = *from++; /* "to" pointer is NOT incremented, see explanation below */
} while(--count > 0);
Run Code Online (Sandbox Code Playgroud)
哪个是Swift中Duff设备的确切实现?
这只是一个语言和编码问题,它不适用于真正的Swift应用程序.
您可以用最高级别的代码表达您的意图,并相信 Swift 编译器会为您优化它,而不是尝试自己优化它。Swift 是一种高级语言。您不会用高级语言进行低级循环展开。
尤其是在 Swift 中,您无需担心复制数组(Duff 设备的原始应用程序),因为每当您分配数组时,Swift 都会使用“写入时复制”来假装复制数组。这意味着只要您只是读取两个变量,它就会使用相同的数组,但一旦您修改其中一个变量,它就会在后台创建一个重复项。
例如,来自https://developer.apple.com/documentation/swift/array 修改数组的副本
Each array has an independent value that includes the values of all
of its elements. For simple types such as integers and other structures,
this means that when you change a value in one array, the value of that
element does not change in any copies of the array. For example:
var numbers = [1, 2, 3, 4, 5]
var numbersCopy = numbers
numbers[0] = 100
print(numbers)
// Prints "[100, 2, 3, 4, 5]"
print(numbersCopy)
// Prints "[1, 2, 3, 4, 5]"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
428 次 |
| 最近记录: |