- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用静态数组时,我注意到根据您转换/创建静态矩阵的方式存在巨大的效率损失:
using StaticArrays
code_fast() = SMatrix{2,2}(1.0 , 1.0 , 1.0 , 1.0); code_fast(); @btime code_fast();
>> 0.026 ns (0 allocations: 0 bytes)
code_slow() = SMatrix{2,2}([1.0 1.0 ; 1.0 1.0]); code_slow(); @btime code_slow();
>> 45.371 ns (1 allocation: 112 bytes)
慢代码可以很容易地纠正,但是,通过设置
code_slow() = @SMatrix [1.0 1.0 ; 1.0 1.0]
它不再慢并且变得和
code_fast
一样快.问题在于使用
@SMatrix
宏必须真正手动编写要转换的矩阵的分量,否则使用
rand
,
zeros
或
ones
.如果这不能做到,那么除了使用
SMatrix{n,n}
之外,我找不到任何选择。 ,这会导致使用静态数组的速度很慢,它们变得毫无用处。
test(n,G,mat) = begin mat[1+n:2n,1:n] = G; mat end
哪里
G
是在另一个函数内计算的复杂静态矩阵。我需要把这个矩阵塞进
mat
作为一个块,我需要输出也是静态的(此操作执行了数十亿次)。通过在最后转换为静态的,即:
test_SA(n,G,mat) = begin mat[1+n:2n,1:n] = G; SMatrix{2n,2n}(mat) end
我遇到了上述
SMatrix
的问题太慢:
n=2
G = @SMatrix ones(Float64,n,n)
mat = Matrix{Float64}(I,2n,2n)
test(n,G,mat); @btime test($n,$G,$mat); # 17.821 ns (0 allocations: 0 bytes)
test_SA(n,G,mat); @btime test_SA($n,$G,$mat_mm); # 1.825 μs (22 allocations: 1.36 KiB)
有人可以解释一下发生了什么,以及如何纠正它?我真的很想在这里使用静态数组,因为代码中的所有其他模块正是因为它们而运行得更快。
最佳答案
像名字一样表示大小的一个StaticArray
应该是静态的,在您的代码中它是动态的而不是静态的。您的问题可以缩小为:
julia> @btime SMatrix{2*$n,2*$n}($mat);
1.310 μs (22 allocations: 1.36 KiB)
该怎么办?第一个选项是具有在编译时已知的固定大小:
julia> @btime SMatrix{4,4}($mat);
1.599 ns (0 allocations: 0 bytes)
这并不总是很方便所以幸运的是还有另一个合理的选择 - 提前创建目标类型(并且可能会重复使用它 10 亿次)。
MySMatrix = SMatrix{2n,2n}
一旦创建,它将非常快速:
julia> @btime $MySMatrix($mat)
1.799 ns (0 allocations: 0 bytes)
4×4 SArray{Tuple{4,4},Float64,2,16} with indices SOneTo(4)×SOneTo(4):
1.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0
1.0 1.0 1.0 0.0
1.0 1.0 0.0 1.0
如果您正在生成静态数组,您还必须查看
sacollect
这也快如 hell :
julia> @btime StaticArrays.sacollect($MySMatrix, i*j for i in 1:4, j in 1:4)
1.399 ns (0 allocations: 0 bytes)
4×4 SArray{Tuple{4,4},Int64,2,16} with indices SOneTo(4)×SOneTo(4):
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
关于optimization - 使用 StaticArrays 有效地填充矩阵中的 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65524097/
我收到未知数据,我想以编程方式查看相关性,并将所有完全相关的变量组合在一起(忽略方向)。在下面的数据集中,我可以手动查看相关性并说 a, f, g, h一起去吧b, d, e .我怎样才能以编程方
这个问题在这里已经有了答案: use dplyr's summarise_each to return one row per function? (3 个答案) 关闭 4 年前。 作为探索性工作的
我想要完成的是使用数组存储未知大小的多项式。我在互联网上看到的是使用一个数组,每个单元格都包含系数,度数是单元格编号,但这不是有效的,因为如果我们有一个多项式,如:6x^14+x+5。这意味着我们将从
嘿伙计们,我一直在尝试解析 HTML 文件以从中抓取文本,但时不时地,我会得到一些非常奇怪的字符,例如 à€œ。我确定是“智能引号”或弯头标点符号导致了我的所有问题,因此我的临时修复是搜索所有这些字符
我原来的 data.table 由三列组成。 site、observation_number 和 id。 例如以下是 id = z 的所有观察结果 |site|observation_number|i
"Premature optimisation is the root of all evil (but it's nice to have once you have an ugly solutio
给定这个数组 X: [1 2 3 2 3 1 4 5 7 1] 和行长度数组R: [3 2 5] 表示转换后每行的长度。 我正在寻找一个计算效率高的函数来将 X reshape 为数组 Y: [[ 1
我有一些 data.frame s: # Sample data a <- data.frame(c(1:10), c(11:20)) names(a) <- c("A", "B") b <- dat
我有点困惑。列表擅长任意位置插入,但不善于随机访问? (怎么可能)如果你不能随机访问,你怎么知道在哪里插入? 同样,如果你可以在任何位置插入,为什么你不能从那个位置高效地读取? 最佳答案 如果您已经有
我有一个向量,我想计算它的移动平均值(使用宽度为 5 的窗口)。 例如,如果有问题的向量是[1,2,3,4,5,6,7,8],那么 结果向量的第一个条目应该是 [1,2,3,4,5] 中所有条目的总和
有一个随机整数生成器,它生成随机整数并在后台运行。需求设计一个API,调用时返回当时的簇数。 簇:簇是连续整数的字典顺序。例如,在这种情况下,10,7,1,2,8,5,9 簇是 3 (1,2--5--
我想做的是将一组 (n) 项分成大小相等的组(大小为 m 的组,并且为简单起见,假设没有剩余,即 n 可以被 m 整除)。这样做多次,我想确保同一组中的任何项目都不会出现两次。 为了使这稍微更具体一些
假设我有一些包含类型排列的模板表达式,在本例中它们来自 Abstract Syntax Tree : template
我已经在这方面工作了几天,似乎没有我需要的答案。 由于担心这个被标记为重复,我将解释为什么其他问题对我不起作用。 使用 DIFFLIB for Python 的任何答案都无助于我的需求。 (我在下面描
我正在使用 NumPy 数组。 我有一个 2N 长度向量 D,并希望将其一部分 reshape 为 N x N 数组 C. 现在这段代码可以满足我的要求,但对于较大的 N 来说是一个瓶颈: ``` i
我有一个问题: 让我们考虑这样的 pandas 数据框: Width Height Bitmap 67 56 59 71 61 73 ...
我目前正在用 C 语言编写一个解析器,设计它时我需要的东西之一是一个可变字符串“类”(一组对表示实例的不透明结构进行操作的函数),我将其称为 my_string。 string 类的实例只不过是包装
假设我在 --pandas-- 数据框中有以下列: x 1 589 2 354 3 692 4 474 5 739 6 731 7 259 8 723
我有一个成员函数,它接受另一个对象的常量引用参数。我想 const_cast 这个参数以便在成员函数中轻松使用它。为此,以下哪个代码更好?: void AClass::AMember(const BC
我们目前正在将 Guava 用于其不可变集合,但我惊讶地发现他们的 map 没有方法可以轻松创建只需稍作修改的新 map 。最重要的是,他们的构建器不允许为键分配新值或删除键。 因此,如果我只想修改一
我是一名优秀的程序员,十分优秀!