- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
最近我注意到滚动的性能正在减慢。我追踪了问题,发现原因是使用了 UIFont(descriptor:size:) 创建的字体。构造函数。我更改了 UIFont(name:size:) 的构造函数我的问题都解决了。
我在 project 中隔离了问题.代码是:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var firstLabel: UILabel!
@IBOutlet weak var secondLabel: UILabel!
@IBAction func onStartTest(sender: AnyObject) {
startMeasurement()
let firstFont = UIFont(name: "Marker Felt", size: 16)
firstLabel.font = firstFont
finishMeasurement("UIFont(name)")
startMeasurement()
let secondFontDescriptor = UIFontDescriptor(name: "Marker Felt", size: 16)
let secondFont = UIFont(descriptor: secondFontDescriptor, size: 16)
secondLabel.font = secondFont
finishMeasurement("UIFont(descriptor)")
}
}
private var time: UInt64 = 0
public func startMeasurement() {
time = mach_absolute_time()
}
public func finishMeasurement(name: String) {
let duration = mach_absolute_time() - time
print("* \(name) \(duration)ns")
}
这些是我的一些测量结果:
iPhone 4S - iOS 9.0.2
* UIFont(name) 111,300ns
* UIFont(descriptor) 112,420,263ns
iPhone 6S - iOS iOS 9.2
* UIFont(name) 134,247ns
* UIFont(descriptor) 17,047,707ns
Simulator - iOS 9.2
* UIFont(name) 1,971,106ns
* UIFont(descriptor) 485,208,205ns
Simulator - iOS 8.1
* UIFont(name) 9,946,584ns
* UIFont(descriptor) 1,957,802,431ns
我做错了什么吗?
最佳答案
我在 WWDC 2018 上问了一位工程师,他基本上是这样告诉我的:
描述符非常非常昂贵,因为它们每次都在整个系统字体库中搜索您提供的特征。他告诉我这个搜索确实没有优化,所以永远不应该在滚动情况下使用,应该只调用一次然后缓存。这样做是因为您实际上不需要指定字体名称,而只需指定字体的各种特性,系统就会为您提供一些东西。
UIFont(name: size:)
但是速度非常快,因为它直接转到系统中请求的字体。它知道字体在哪里,因为你已经给出了完整的确切名称,然后它可以缩放它。没有其他好的功能。
这大致是一个 O(n) 操作与一个 O(1)
关于ios - 为什么 UIFont(描述符 :size:) is x200 slower than UIFont(name:size:)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34954956/
ValueError Traceback (most recent call last) in 23 out
在 CSS 中,我从来没有真正理解为什么会发生这种情况,但每当我为某物分配 margin-top:50% 时,该元素就会被推到页面底部,几乎完全消失这一页。我假设 50% 时,该元素将位于页面的中间位
我正在尝试在 pyTorch 中训练我的第一个神经网络(我不是程序员,只是一个困惑的化学家)。 网络本身应该采用 1064 个元素向量并用 float 对它们进行评级。 到目前为止,我遇到了各种各样的
我有一个简单的问题。如何在 3 个维度上移动线性阵列?这似乎太有效了,但在 X 和 Y 轴上我遇到了索引问题。我想这样做的原因很简单。我想创建一个带有 block 缓冲区的体积地形,所以我只需要在视口
我正在尝试运行我购买的一本关于 Pytorch 强化学习的书中的代码。 代码应该按照本书工作,但对我来说,模型没有收敛,奖励仍然为负。它还会收到以下用户警告: /home/user/.local/li
我目前正在使用 this repo使用我自己的数据集执行 NLP 并了解有关 CNN 的更多信息,但我一直遇到有关形状不匹配的错误: ValueError: Target size (torch.Si
UIScrollView 以编程方式设置,请不要使用 .xib 文件发布答案。 我的 UIScrollView 位于我的模型类中,所以我希望代码能够轻松导入到另一个项目中,例如。适用于 iPad 或旋
我在我的 Ruby on Rails 应用程序(版本 4.3.1)中使用 Bootstrap gem。我最近发现了响应式字体大小功能 (rfs)。根据 Bootstrap 文档,它刚刚在 4.3 版中
这个问题不太可能帮助任何 future 的访客;它仅与一个小地理区域、一个特定时刻或一个非常狭窄的情况相关,而这些情况通常不适用于互联网的全局受众。如需帮助使这个问题更广泛地适用,visit the
size 之间的语义区别是什么?和 sizeIs ?例如, List(1,2,3).sizeIs > 1 // true List(1,2,3).size > 1 // true Luis 在 c
我想从 div 中删除一些元素属性。我的 div 是自动生成的。我想遍历每个 div 和子 div,并想删除所有 font-size (font-size: Xpx)和 size里面font tag
super ,对 Python 和一般编程 super 新手。我有一个问题应该很简单。我正在使用一本使用 Python 3.1 版的 python 初学者编程书。 我目前正在写书中的一个程序,我正在学
我无法从 NativeBase 更改缩略图的默认大小。我可以显示默认圆圈,即小圆圈和大圆圈,但我想显示比默认大小更大的圆圈。这是我的缩略图代码: Prop 大小不起作用,缩略图仍然很小。 我的 Na
我是pytorch的新手。在玩张量时,我观察到了两种类型的张量- tensor(58) tensor([57.3895]) 我打印了它们的形状,输出分别是 - torch.Size([]) torch
这是我的 docker images 命令的输出: $ docker images REPOSITORY TAG IMAGE ID CREATED
来自 PriorityQueue 的代码: private E removeAt(int i) { assert i >= 0 && i < size; modCount++;
首先,在我的系统上保留以下内容:sizeof(char) == 1 和 sizeof(char*) == 4。很简单,当我们计算下面类的总大小时: class SampleClass { char c
我正在编写一个游戏来查找 2 个图像之间的差异。我创建了 CCSprite 的子类 Spot。首先我尝试创建小图像并根据其位置添加自身,但后来我发现位置很难确定,因为很难避免 1 或 2 个像素的偏移
我有一个 Tumblr Site每个帖子的宽度由标签决定。 如果一篇文章被标记为 #width200,CSS 类 .width200 被分配。 问题是,虽然帖子的宽度不同,但它们都使用主题运算符加载相
这个问题在这里已经有了答案: What is the ideal growth rate for a dynamically allocated array? (12 个答案) 关闭 8 年前。 我
我是一名优秀的程序员,十分优秀!