Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Duff的设备在Swift中大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我们知道 Duff’s device使用隔行开关和循环的结构交错,如: send(to, from, count) register short *to, *from; register count; { register n = (count + 7) / 8; switch (count % 8) { case 0: do { *to = *from++; ca
我们知道 Duff’s device使用隔行开关和循环的结构交错,如:
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);
    }
}

现在,在Swif 2.1中,@L_419_1@并没有像我们在Swift文档中读到的那样隐含起来:

现在,鉴于在Swift中明确存在一个突破性副作用,有一个通过条款:

这非常像:

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."

虑到维基百科提醒我们,这些设备来自这个问题

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);

哪个是Swift中Duff设备的确切实现?

这只是一种语言和编码问题,它不适用于真正的Swift应用程序.

您可以在最高级别的代码中表达您的意图,并信任Swift编译器为您优化它,而不是尝试自己优化它. Swift是一种高级语言.您不需要使用高级语言进行低级循环展开.

特别是在Swift中,您不必担心复制数组(Duff设备的原始应用程序),因为Swift假装在分配数组时使用“copy on write”复制数组.这意味着只要您只是从它们中读取它就会对两个变量使用相同的数组,但只要您修改其中一个变量,它就会在后台创建一个副本.

例如,从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,4,5]
var numbersCopy = numbers
numbers[0] = 100
print(numbers)
// Prints "[100,5]"
print(numbersCopy)
// Prints "[1,5]"

大佬总结

以上是大佬教程为你收集整理的Duff的设备在Swift中全部内容,希望文章能够帮你解决Duff的设备在Swift中所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。